Ruby regex syntax for "not matching one of the following" -
nice simple regex syntax question you.
i have block of text , want find instances of href=" or href=' not followed either [ or http://
i can "not followed [" with
record.body =~ /href=['"](?!\[)/
and can "not followed http://"
record.body =~ /href=['"](?!http\:\/\/)/
but can't quite work out how combine two.
just clear: want find bad strings this
`href="www.foo.com"` but i'm ok (ie don't want find) strings this
`href="http://www.foo.com"` `href="[registration_url]"`
combine both using alternation operator.
href=['"](?!http\:\/\/|\[) for more specific, be.
href=(['"])(?!http\:\/\/|\[)(?:(?!\1).)*\1 this handle both single quoted or double quoted string in href part. , won't match strings href='foo.com" or href="foo.com' (unmatched quotes)
(['"]) capture double quote or single quote. (?!http\:\/\/|\[) , matched quote won't followed http:// or [, if yes, moves on next pattern. (?:(?!\1).)* matches character not of captured character, 0 or more times. \1 followed captured character.
Comments
Post a Comment