Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex Matching - A letter not preceded by another letter

Tags:

python

regex

What could be regex which match anystring followed by daily but it must not match daily preceded by m?

For example it should match following string

  • beta.daily
  • abcdaily
  • dailyabc
  • daily

But it must not match

  • mdaily or
  • abcmdaily or
  • mdailyabc

I have tried following and other regex but failed each time:

  • r'[^m]daily': But it doesn't match with daily
  • r'[^m]?daily' : It match with string containing mdaily which is not intended
like image 950
Shamshad Alam Avatar asked Aug 30 '16 05:08

Shamshad Alam


People also ask

What does .*?) Mean in regex?

(. *?) matches any character ( . ) any number of times ( * ), as few times as possible to make the regex match ( ? ). You'll get a match on any string, but you'll only capture a blank string because of the question mark.

How do I not match a character in regex?

There's two ways to say "don't match": character ranges, and zero-width negative lookahead/lookbehind. Also, a correction for you: * , ? and + do not actually match anything. They are repetition operators, and always follow a matching operator.

What does the regex 0 9 ]+ do?

In this example, [0-9] matches any SINGLE character between 0 and 9 (i.e., a digit), where dash ( - ) denotes the range. The + , known as occurrence indicator (or repetition operator), indicates one or more occurrences ( 1+ ) of the previous sub-expression. In this case, [0-9]+ matches one or more digits.

What does regex 0 * 1 * 0 * 1 * Mean?

Basically (0+1)* mathes any sequence of ones and zeroes. So, in your example (0+1)*1(0+1)* should match any sequence that has 1. It would not match 000 , but it would match 010 , 1 , 111 etc. (0+1) means 0 OR 1.


1 Answers

Just add a negative lookbehind, (?<!m)d, before daily:

(?<!m)daily

The zero width negative lookbehind, (?<!m), makes sure daily is not preceded by m.

Demo

like image 158
heemayl Avatar answered Sep 20 '22 16:09

heemayl