Home > Enterprise >  Find exact match with regular expression without preleading string in python
Find exact match with regular expression without preleading string in python

Time:07-15

I have a text file called my_file.txt that has the following content:

                                  R.A.O.S-VARIATION WITH WAVE PERIOD/FREQUENCY
                              VEL R.A.O.S-VARIATION WITH WAVE PERIOD/FREQUENCY
                              ACC R.A.O.S-VARIATION WITH WAVE PERIOD/FREQUENCY
                              SOME OTHER STRING 

Now, I want to find the string R.A.O.S-VARIATION WITH WAVE PERIOD/FREQUENCY without any of the two other choises that have either VEL or ACC in it using a regular expression. There are white spaces before those strings.

How do I get the correct match using:

re.search(regex, line)

How should the regular expression look like that finds the desired string, without the two other options? I prefer to not specify all preleading white spaces in the string.

CodePudding user response:

What you can do is remove the leading whitespace using the .lstrip() function and then search for the string.

re.search(regex, line.lstrip())

CodePudding user response:

re.findall(r'R.A.O.S-VARIATION WITH WAVE PERIOD/FREQUENCY', line)

  • Related