
Misc
Contents
What's New
|
Regex Thisby justin carlson on 01/11/2011Some commonly used regex examples.First, to match something that isn't the regex, aka What is the regular expression to not match a pattern, or how do I regex for something that doesn't match: ^(?!regexhere).*$
The example above would match on a string that does not equal "regexhere".
^(?!192\.168\.1\.1).*$
Below is a quick example to do simple IP validation: Long way: ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$
Short way: ^([0-9]{1,3}(.[^$]|$)){4} That's just: ^ (starts with) [0-9]{1,3} the numbers 0-9 in length of 1 to 3, 4 times delimited by decimals, ending in a decimal or the end of the string, but not ending in a decimal at the end of the string.
Lookahead / Lookbehind / Lookaround Example
(?<!href)="http://[^\s]+"
The expression above will match anything pointing to an http:// url except links. It does this by matching a url attribute ="http://[^\s]+" that is not preceded by href (ie: src=, etc) . References to urls that are not attributes should be ignored. This is useful if you want to search for images that are not https, etc.
TrackbackTrackback URL for this entry: http://www.tehuber.com/trackback.php?id=20110111155434132 No trackback comments for this entry. |