Michiel V.
Michiel V.

Reputation: 141

Define end and beginning of regular expression

Say I have a list of procedure codes like the following:

  procedure_codes = ['038911.B', '32431.1', 'fdsfd.B', '13635.1B', '78935.1b']

In python I now want to extract all codes:

To me the most logical approach seemed using a regular expression, so I tried

combined (for each element in the list):

  for i in procedure_codes:
      if re.search('[^0]\d+\.\d[^bB]?', i):
          print(i)

I would suspect python to only return the code: '32431.1' but instead the return is :

And thus it seems like the final negation is just completely ignored

Since I'm completely new to using regular expressions I do not know if I am simply making a syntax error or if I have misunderstood regular expressions completely.

Upvotes: 0

Views: 81

Answers (1)

Daria Pydorenko
Daria Pydorenko

Reputation: 1802

Just wrap your regex with ^ and $ to match beginning of line and end of line respectively:

^[^0]\d+\.\d[^bB]?$

Because without them, it finds 13635.1 group in 13635.1B and the same with 78935.1b.

Upvotes: 2

Related Questions