0

I have a file with lines like this

variable = epms[something][something]

I need to find these lines by searching for epms.

Currently, I am trying this:

regex = re.compile('[.]*epms\[[.]*\]\[[.]*\][.]*')

However this doesn't find any match. What am I doing wrong?

fangio
  • 1,746
  • 5
  • 28
  • 52

3 Answers3

2

You can use pattern:

epms\[[^]]+\]\[[^]]+\]
  • epms Match literal substring.
  • \[ Match a [.
  • [^]]+ Negated character set. Anything other than a ].
  • \] Match a ].
  • \[ Match a [.
  • [^]]+ Negated character set. Anything other than a ].
  • \] Match a ].

In Python:

import re

mystring = "variable = epms[something][something]"
if re.search(r'epms\[[^]]+\]\[[^]]+\]',mystring):
    print (mystring)
Paolo
  • 21,270
  • 6
  • 38
  • 69
1

Try this pattern epms\[.*\]\[.*\].

Ex:

import re

with open(filename1) as infile:
    for line in infile:
        if re.search(r"epms\[.*\]\[.*\]", line):
            print(line)
Rakesh
  • 81,458
  • 17
  • 76
  • 113
0

Try this, tested in Python3:

>>> s = 'variable = epms[something][something]'
>>> re.match(r'.*epms\[.*\]\[.*\]', s)
<_sre.SRE_Match object; span=(0, 37), match='variable = epms[something][something]'>

You don't need square brackets to identify 'any characters'.

W.Kai
  • 78
  • 2
  • 6