简体   繁体   中英

Re.search in python regex not working as intended

QUESTION:

I am a beginner with python and using regex engine of python. I am able to match several sample regex patterns with my seed file but weirdly enough, I am unable to match lines in my sample file that contains the word "repeat." Below is the context of my issue. What could be the reason?

Sample Text:

import tset flash_read, flash_writ;

vector ( $tset, (XMOSI,XMISO,XSGLK,XSTRMSTRT,XSTRMSGLK,XSTRMGKEN,XXTALIN,XXTALGPUEN,XHV (XSTRM03,XSTRMO2,XSTRM01,XSTRIADO,XNSS3,XNSS2,XNSS1,XNSSOH, XTEGLOGK, XRXDATA, XRXENABLE, XTXDATA, XTXENABLE, XNRESET, ROOK, XTMS, XTDI, XTDO, XNTRST))

> flash_writ .d0000 .dFF 1 01 01 01 01 X 1; // write byte 0

> flash_writ .d0001 .dFF 1 0 1 0 1 01 01 X 1; // write byte 1

repeat 25> flash_writ .d0000 .d00 1 1 1 0001  0 1 X 1; // wait program time 
> flash_writ .d0002 .dFF 1 0 1 0 1 0 1 0 1 X 1; // write byte 0

> flash_writ .d0003 .dFF 1 0 1 0 1 0 1 0 1 X 1; // write byte 1

repeat 25> flash_writ .d0000 .d00 1 1 1 0001  0 1 X 1; // wait program time 
> flash_writ .d0004 .dFF 1 01 01 01 01 X1, 11 write byte 0

> flash_writ .d0005 .dFF 1 01 01 01 01 X1; // write byte 1

repeat 25> flash_writ .d0000 .d00 1 1 1 0001  0 1 X 1; // wait program time

Python Syntax Used for regex search:

regex_rep = r” repeat "

for num, eachline in enumerate(files_atp):

if re.search(regex_rep, eachline, flags=re.IGNORECASE) is not None:

      print eachline

THIS IS NOT WORKING (does not produce any matches)

Your pattern is:

regex_rep = r" repeat "

This will match the word repeat with a space on each end.

But your lines look like this:

repeat 25> flash_writ .d0000 .d00 1 1 1 0001  0 1 X 1; // wait program time

There's no space before repeat , so it doesn't match your pattern.

It's hard to suggest how to fix this, because I'm not sure why you put those spaces in the pattern in the first place.


If they're there for no reason, just get rid of them:

regex_rep = r"repeat"

However, in that case, you're not using any features of re at all, so your test would be better written as:

if "repeat" in eachline:

If they're there to make the pattern more readable, and you want re to ignore the spaces, you can use the VERBOSE flag to tell it to ignore spaces in your pattern:

if re.search(regex_rep, eachline, flags=re.IGNORECASE|re.VERBOSE) is not None:

You can see this working at regex101 .


If you want to make sure you're matching repeat as a whole word, rather than as part of a larger word like repeatable , you can use the \\b special character, which:

Matches the empty string, but only at the beginning or end of a word…

regex_rep = r"\brepeat\b"

You can see this in action at regex101 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM