Regular expression escape warning - ruby

For code
url.gsub(/"|\[|]| /, '')
ruby raises warning
warning: regular expression has ']' without escape: /"|\[|]| /
How to fix it?

Your regex would be reduced to,
url.gsub(/[ "\[\]]/, '')

Related

Silence Ruby regular expression warning

I have a - in my regular expression. When I run it, I get warning: character class has '-' without escape: /[^a-zA-Z0-9-_\\.]/
How can I silence this warning?
At this place [^a-zA-Z0-9-_\\.] there is a possibility of an error. Usage of '-' either means the new range(with mistyping) or just a symbol. Either move single characters in front of ranges or prefix with '\'
verbosity = $VERBOSE
$VERBOSE = nil
/[^a-zA-Z0-9-_\\.]/ #=> /[^a-zA-Z0-9-_\\.]/
$VERBOSE = verbosity
Add a backslash (\) to escape the lonely dash. It appears the offending dash is immediately after [^a-zA-Z0-9. Add \- instead of just the dash character -. The full regex including the minor change is
/[^a-zA-Z0-9\-_\\.]/
This will remove the warning. Escaping it with the backslash clearly instructs the regex engine that you mean to search for that character and not that you forgot to complete a range like [a-z]

Preg_ match using?

I use preg_match to the program like this
if (preg_match('/^[a-z0-9]+\:/{1,2}', $filename))
But it shows an error like this
Warning: preg_match() [function.preg-match]: Unknown modifier '{'
how to change this?
You are missing '/' at the end of the regex and you should escape '/' in the regex itself. This should work i.e. warning should be gone (ignoring if regex you've written is doing what you want):
if (preg_match('/^[a-z0-9]+\:\/{1,2}/', $filename))

How to use şŞıİçÇöÖüÜĞğ characters in a regular expression in Ruby?

I tried using a regular expression to capture names:
r[1].scan(/^([A-Z]|[ŞİÇÖÜĞ])([a-z]|[şŞıİçÇöÖüÜĞğ])*\s([A-Z]|[ŞİÇÖÜĞ])([a-z]|[şŞıİçÇöÖüÜĞğ])*/u)
But, it gives me an error:
syntax error, unexpected $end, expecting ')'
...atches = r[1].scan(/^([A-Z]|[ŞİÇÖÜĞ])([a-z]|[şŞ�...
...
I see that the problem is the Turkish characters I'm using. Is it possible to use unicode values of the characters in regexp? How can I use these problematic characters in this regexp?
Use ruby 1.9
Go with /\p{Word}+\p{Space}\p{Word}*/

preg_match warning

I have deprecated "eregi" problem.
if (eregi("data/cheditor4[^<>]*\.(gif|jp[e]?g|png|bmp)", $edit_img, $tmp))
So I changed into this,
if (preg_match("/data/cheditor4[^<>]*\.(gif|jp[e]?g|png|bmp)/i", $edit_img,$tmp))
But I got new warmingmessage,
Warning: preg_match() [function.preg-match]: Unknown modifier 'c'
Please let me know what is wrong.
Thanks in advance.
You have a '/' inside your regex ('data/cheditor') but you are also using '/' as the regex delimiter ('/myregex/flags'): you can either escape the internal '/', or use a different regex delimiter.
E.g. first option:
preg_match('/data\/cheditor4[^<>]*\.(gif|jp[e]?g|png|bmp)/i',...
or (second option, I chose '#' as the delimiter):
preg_match('#data/cheditor4[^<>]*\.(gif|jp[e]?g|png|bmp)#i',...
Also note I changed the " around your regex to ' because otherwise you need to double the backslashes within double-quotes in PHP.

negative lookbefore for a blackslash in ruby 1.9

I want to match every '[' or ']' that's not preceded by a backslash in ruby 1.9
I tried:
/?<!\134[\[\]]/
and
/?<!\\\\[\[\]]/
but I get a 'target of repeat operator not specified'
You will need to wrap the negative lookbehind in parenthesis.
(?<!\\)[\[\]]

Resources