Regular expression negative lookahead

A negative lookahead says, at this position, the following regex can not match. Let’s take a simplified example: The last example is a double negation: it allows b followed by c. The nested negative lookahead becomes a positive lookahead: the c should be present. In each example, only the a is matched. The lookahead is only a condition, and does not add to … Read more

Regex to test if string begins with http:// or https://

our use of [] is incorrect — note that [] denotes a character class and will therefore only ever match one character. The expression [(http)(https)] translates to “match a (, an h, a t, a t, a p, a ), or an s.” (Duplicate characters are ignored.) Try this: If you really want to use alternation, use this syntax instead:

python re.split() to split by spaces, commas, and periods, but not in cases like 1,000 or 1.50

Use a negative lookahead and a negative lookbehind: In other words, you always split by \s (whitespace), and only split by commas and periods if they are not followed (?!\d) or preceded (?<!\d) by a digit. DEMO. EDIT: As per @verdesmarald comment, you may want to use the following instead: This will split “1.2,a,5” into [“1.2”, “a”, “5”]. DEMO.

python re.split() to split by spaces, commas, and periods, but not in cases like 1,000 or 1.50

Use a negative lookahead and a negative lookbehind: In other words, you always split by \s (whitespace), and only split by commas and periods if they are not followed (?!\d) or preceded (?<!\d) by a digit. DEMO. EDIT: As per @verdesmarald comment, you may want to use the following instead: This will split “1.2,a,5” into [“1.2”, “a”, “5”]. DEMO.

Regex AND operator

It is impossible for both (?=foo) and (?=baz) to match at the same time. It would require the next character to be both f and b simultaneously which is impossible. Perhaps you want this instead: This says that foo must appear anywhere and baz must appear anywhere, not necessarily in that order and possibly overlapping (although overlapping is not possible in this specific case because the letters … Read more

grep –ignore-case –only

This is a known bug on the initial 2.5.1, and has been fixed in early 2007 (Redhat 2.5.1-5) according to the bug reports. Unfortunately Apple is still using 2.5.1 even on Mac OS X 10.7.2. You could get a newer version via Homebrew (3.0) or MacPorts (2.26) or fink (3.0-1). Edit: Apparently it has been fixed on OS X 10.11 (or maybe earlier), even … Read more

Regular Expression for alphanumeric and underscores

To match a string that contains only those characters (or an empty string), try This works for .NET regular expressions, and probably a lot of other languages as well. Breaking it down: If you don’t want to allow empty strings, use + instead of *. As others have pointed out, some regex languages have a shorthand form … Read more