1

How can I match exact strings in Python which can dynamically catch/ignore the following cases?

If I want to get value2 in this IE output "Y" from a file that is formatted as such:

...
--value1 X
---value2 Y
----value3 Z
....

How can I search for the exact match "value2" whilst ignoring the preceding "---", these characters don't allow exact string matching with the "==" operator when searching each line.

cc6g11
  • 477
  • 2
  • 10
  • 24

4 Answers4

0

You could strip leading dashes, then split the result to get the first word without the dashes:

let's say you iterate on the lines:

for line in lines:
    first_word = line.lstrip("-").split()
    if first_word == "value2":
        print("found")

regex can be of help too, with word boundary on the right

if re.match(r"^-*value2\b",line):
Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
0

You can remove the extra characters at the start of a string s using s.lstrip('-') before using an exact match. There are other ways to handle this, but this is the fastest and strictest way without using regular expressions.

Christoph Burschka
  • 4,467
  • 3
  • 16
  • 31
0

Can you guarantee that all of the valid words with have a dash before them and a space afterward? If so, you could write that like:

for line in lines:
    if '-value2 ' in line:
        print(line.split()[1])
Kirk Strauser
  • 30,189
  • 5
  • 49
  • 65
0

The simplest way that I know is:

for line in lines:
  if 'value2' in line:
    ...

Another way (if you need to know position):

for line in lines:
  pos = line.find('value2')
  if pos >= 0:
     ...

More complex things can be done as well, like a regular expression, if necessary, but without knowing what validation you need. The two ways above, I feel, are the most simple.

UPDATE (addressing comment): (Trying to keep it simple, this requires a space after the number)

for line in lines:
  for token in line.split():
    if 'value2' in token:
      ...
gchaber
  • 9
  • 1
  • `if 'value2' in line` doesn't do exact word match. Try with `value222` – Jean-François Fabre Mar 11 '19 at 16:59
  • This is true, but I wasn't sure what the op needed it here, so I gave two very simple suggestions. I'll edit my post with a quick alternative (assuming that a space is going to be required after). Anything more complicated than this, and I would move to the solutions others posted or use a regular expression. – gchaber Mar 11 '19 at 17:05