Replace a exact string using Regex in ruby - ruby-on-rails

Consider the the following text
The capital asset as defined in section 2(14) is an exhaustive definition which encompasses all properties of any kind with certain exceptions but the key word is that the property should be "held" section 2.
Now I want to find section 2, for the same I have written the following Regex:
/\bsection+\.*\s+2\b/i
But it is also matching section 2 of section 2(14). I just want it to only match the exact text, not the part of text which is matching with the regex. I know I need to modify the regex, but what are the changes required?

Try with \bsection+\.*\s+2([ .,;?|!])/i . This will only match with section 2 if it is followed by a space or a punctuation mark different than (.

/\bsection+.*\s+2\b([[:punct:]]|\s/
basically you would want the word to end with whitespace or a punctuation.

Related

Pattern Layout to abbreviate the name of all logger components except the 2 rightmost

The following are my favorite patterns
%c{2} - which displays the corresponding number of rightmost logger
name components. So a logger with a name like "org.apache.commons.Foo"
is displayed as "commons.Foo".
and
%c{1.} - which abbreviates the name based on the pattern. So a logger
with a name like "org.apache.commons.Foo" is displayed as "o.a.c.Foo".
Is there a way to combine the two. I want to abbreviate the name of all logger components except the 2 rightmost. Such that "org.apache.commons.Foo" is displayed as "o.a.commons.Foo".
Is it possible to do this using the pattern layout or in any other easy way?
Yes and no. NameAbbreviator.java handles creating the abbreviation of the name. It has a getAbbreviator method that returns one of 3 implementations -
NoOp - returns the string as is.
MaxElement - returns the last n elements.
Pattern - This uses the pattern to break up the string into fragments, formatting each according to the pattern. If you specify 1.2 then the first character of the first item will be printed and 2 characters of each following item will be printed up to the final token, which is never abbreviated. A special case is if the pattern contains a "*". That indicates that everything that follows should be printed as is.
So the answer to your question is that you could use the following patterns:
1.* - would print o.apache.commons.Foo
1.1.* - would print o.a.commons.Foo
As you can see this isn't very helpful when package names have different lengths. I would suggest you create a Jira issue for this. Patches and pull requests are welcome!

get text with whitespaces when generate grammar

Is it possible to get a text with whitespaces?
Example rule:
rule: 'text' text+=ID+
I can obtain the text as a list and could add a whitespace programmatically for each list element, but i don't want that approach. I actually don't need a list. I want one single variable holding the text with whitespaces.
STRING works fine, but it has those ugly quotes, i don't want those quotes as well.
Maybe someone has an idea how to achieve that?
You can use a Datatype-Rule like this:
rule: 'text' text+= MyText ;
MyText:
ID+
;
Lookup some details in the Reference Documentation in the section Data Type Rules.

How do you do wildcard searches with Mongoid in a Ruby on Rails environment?

The Mongoid documentation only gives one example of doing a wildcard search:
Person.where(first_name: /^d/i)
This finds all people with the first name that starts with "d".
What do the /^ and /i represent?
How do I find all people with their first name having an "na" in the middle of the string? E.g., this query would find "jonathan" since "na" is a substring of the entire string.
Is there website or guide with this information?
You need this to find people with "na" in the name.
Person.where(first_name: /na/i)
As for your example:
Person.where(first_name: /^d/i)
^ means "beginning of the line". This regex will match all strings where first letter is "d". /i means "do case-insensitive matches". So it'll match both "d" and "D".
Note: only prefix regexes (with ^ in front) are able to use indexes.
Is there website or guide with this information?
Here's my favourite.
This is not a "wildcard" search, this is called a regular expression.
/^d/i
The two slashes are only the regex delimiters, you search for what is in between those two slashes.
The following i is a modifier or option. It changes the matching behaviour of your regex, the i stands for case insensitive, means it matches "d" and "D".
The first character ^ is an anchor, it anchors the search pattern to the start of the string, means match "d" only at the start of the string
A good tutorial about regular expressions is the tutorial on regular-expressions.info
If you want to search for a string anywhere in the string, just remove the anchor that binds the pattern to the start, /na/ will find "na" anywhere in the string.

How can I update parts of the string that matches some regexp

I have string "(1,2,3,4,5,6),(1,2,3)" I would like to change it to "('1','2','3','4','5','6'),('1','2','3')" - replase all parts that mathces /([^,)("])/ with the '$1', '$2' etc
"(1,2,3,4,5,6),(1,2,3)".gsub(/([^,)("]\w*)/,"'\\1'")
gsub is a "global replace" method in String class. It finds all occurrences of given regular expression and replaces them with the string given as the second parameter (as opposed to sub which replaces first occurrence only). That string can contain references to groups marked with () in the regexp. First group is \1, second is \2, and so on.
Try
mystring.gsub(/([\w.]+)/, '\'\1\'')
This will replace numbers (ints/floats) and words with their "quote-surrounded" selves while leaving punctuation (except the dot) alone.
UPDATED: I think you want to search for this
(([^,)("])+)
And replace it with this
'$1'
the looks for anything 1 or more times and assigns it to the $1 variable slot due to using the parenthesis around the "\d". The replace part will use what it finds as the replacement value.

Rails validates_format_of

I want to use validates_format_of to validate a comma separated string with only letters (small and caps), and numbers.
So.
example1, example2, 22example44, ex24
not:
^&*, <> , asfasfsdafas<#%$#
Basically I want to have users enter comma separated words(incl numbers) without special characters.
I'll use it to validate tags from acts_as_taggable_on. (i don't want to be a valid tag for example.
Thanks in advance.
You can always test out regular expressions at rubular, you would find that both tiftiks and Tims regular expressions work albeit with some strange edge cases with whitespace.
Tim's solution can be extended to include leading and trailing whitespace and that should then do what you want as follows :-
^\s*[A-Za-z0-9]+(\s*,\s*[A-Za-z0-9]+)*\s*$
Presumably when you have validated the input string you will want to turn it into an array of tags to iterate over. You can do this as follows :-
array_var = string_var.delete(' ').split(',')
^([a-zA-Z0-9]+,\s*)*[a-zA-Z0-9]+$
Note that this regex doesn't match values with whitespace, so it won't match multiple words like "abc xyz, fgh qwe". It matches any amount of whitespace after commas. You might not need ^ or $ if validates_format_of tries to match the whole string, I've never used Rails so I don't know about that.
^[A-Za-z0-9]+([ \t]*,[ \t]*[A-Za-z0-9]+)*$
should match a CSV line that only contains those characters, whether it's just one value or many.

Resources