Regex lookahead, lookbehind and atomic groups

Examples Given the string foobarbarfoo: You can also combine them: Definitions Look ahead positive (?=) Find expression A where expression B follows: Look ahead negative (?!) Find expression A where expression B does not follow: Look behind positive (?<=) Find expression A where expression B precedes: Look behind negative (?<!) Find expression A where expression B does not precede: Atomic … Read more

Regex how to match an optional character

Use to make the letter optional. {1} is redundant. (Of course you could also write [A-Z]{0,1} which would mean the same, but that’s what the ? is there for.) You could improve your regex to And, since in most regex dialects, \d is the same as [0-9]: But: do you really need 11 separate capturing groups? And if so, why don’t you capture the … Read more

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

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)