negative lookbefore for a blackslash in ruby 1.9 - ruby

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.
(?<!\\)[\[\]]

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]

Regular expression escape warning

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(/[ "\[\]]/, '')

How to match brackets with regex in ruby

I am currently trying to create a regex that will find this pattern /- - [/. However, since the [ is a special character, I am getting this error:
premature end of char-class: /- - [/ (SyntaxError)
I know this is happening because the compiler is expecting the [ in the regex to close, but I need it to match that in my pattern. How can I do this?
Yes, brackets are part of regex syntax. If you want to match a literal bracket (or any other special symbol, for that matter), escape with a backslash.
'foo[bar]' =~ /\[/ # => 3

Ruby 1.9 unicode escapes in Regexp

I just upgraded an old project to Ruby 1.9.3. I'm having a bunch of trouble with unicode strings. It boils down to:
p = "\\username"; "Any String".match(/#{p}/)
That works in 1.8, and returns nil as expected. However, in 1.9 it throws:
ArgumentError: invalid Unicode escape
I'm trying to match '\u' in a string. I thought the two backslashes will escape it from registering as a unicode.
What am I missing here?
Edit: Single quotes don't work too:
1.9.3p429 :002 > p = '\\username'; "Any String".match(/#{p}/)
ArgumentError: invalid Unicode escape
from (irb):2
When you do /#{p}/ it means p will be interpreted as a regular expression. Since your p is now equal to \username, then this Regexp compilation will fail (since it IS an invalid Unicode escape sequence):
>> Regexp.new "\\username"
RegexpError: invalid Unicode escape: /\username/
I.e. doing /#{p}/ is equal to writing /\username/.
Therefore you have to escape p from any regular expressions so it will be interpreted correctly:
"Any String".match(/#{Regexp.escape(p)}/)
Or just:
"Any String".match(Regexp.escape(p))

Regular expression "empty range in char class error"

I got a regex in my code, which is to match pattern of url and threw error:
/^(http|https):\/\/([\w-]+\.)+[\w-]+([\w- .\/?%&=]*)?$/
The error was "empty range in char class error". I found the cause of that is in ([\w- .\/?%&=]*)? part. Ruby seems to recognize - in \w- . as an operator for range instead of a literal -. After adding escape to the dash, the problem was solved.
But the original regular expression ran well on my co-workers' machines. We use the same version of osx, rails and ruby: Ruby version is ruby 1.9.3p194, rails is 3.1.6 and osx is 10.7.5. And after we deployed code to our Heroku server, everything worked fine too. Why did only my environment have error regarding this regex? What is the mechanism of Ruby regex interpreting?
I can replicate this error on Ruby 1.9.3p194 (2012-04-20 revision 35410) [i686-linux], installed on Ubuntu 12.04.1 LTS using rvm 1.13.4. However, this should not be a version-specific error. In fact, I'm surprised it worked on the other machines at all.
A a simpler demonstration that fails just as well:
"abcd" =~ /[\w- ]/
This is because [\w- ] is interpreted as "a range beginning with any word character up to space (or blank)", rather than a character class containing a word, a hyphen, or a space, which is what you had intended.
Per Ruby's regular expression documentation:
Within a character class the hyphen (-) is a metacharacter denoting an inclusive range of characters. [abcd] is equivalent to [a-d]. A range can be followed by another range, so [abcdwxyz] is equivalent to [a-dw-z]. The order in which ranges or individual characters appear inside a character class is irrelevant.
As you saw, prepending a backslash escaped the hyphen, thus changing the nature of the regexp from a range to a character class, removing the error. However, escaping the hyphen in the middle of character class is not recommended, since it's easy to confuse the intended meaning of the hyphen in such cases. As m.buettner pointed out, always place hyphens either at the beginning or the end of a character class:
"abcd" =~ /[-\w ]/

Resources