1

my problem is that I need to find multiple elements in one string.

For example I got one string that looks like this:

line = if ((var.equals("INPUT")) || (var.equals("OUTPUT"))

and then i got this code to find everything between ' (" ' and ' ") '

char1 = '("'
char2 = '")'


add = line[line.find(char1)+2 : line.find(char2)]
list.append(add)

The current result is just:

['INPUT']

but I need the result to look like this:

['INPUT','OUTPUT', ...]

after it got the first match it stopped searching for other matches, but I need to find everything in that string that matches this search.

I also need to append every single match to the list.

1
  • @Ev.Kounis my bad Commented Nov 23, 2018 at 8:40

4 Answers 4

6

The simplest:

>>> import re
>>> s = """line = if ((var.equals("INPUT")) || (var.equals("OUTPUT"))"""
>>> r = re.compile(r'\("(.*?)"\)')
>>> r.findall(s)
['INPUT', 'OUTPUT']

The trick is to use .*? which is a non-greedy *.

Sign up to request clarification or add additional context in comments.

5 Comments

This is the way to do it. +1
Thanks, im using this now.
I got one more thing, how can i get the result of findall into a variable? If i try result = r.findall(s) i get this error TypeError: expected string or bytes-like object
Your code should work. Looks more like an encoding issue. You're Python 2 or Python 3? Check the type of your input. With something like print(my_input.__class__)
I solved it, i was reading lines from a file but forgot to assign a variable to it^^
2

You should look into regular expressions because that's a perfect fit for what you're trying to achieve.

Let's examine a regular expression that does what you want:

import re
regex = re.compile(r'\("([^"]+)"\)')

It matches the string (" then captures anything that isn't a quotation mark and then matches ") at the end.

By using it with findall you will get all the captured groups:

In [1]: import re

In [2]: regex = re.compile(r'\("([^"]+)"\)')

In [3]: line = 'if ((var.equals("INPUT")) || (var.equals("OUTPUT"))'

In [4]: regex.findall(line)
Out[4]: ['INPUT', 'OUTPUT']

3 Comments

NB: Will not work if there's a " in the string that he wants to find. Non-greedy star operator *? is the clean way to go there.
That was intentional though
Regular experssions are not generally a good fit for brackets matching. For this particular task it works, but it can break easily (or get messy) if doing something slightly more complicated. Just a heads up to the OP
0

If you don't want to use regex, this will help you.

line = 'if ((var.equals("INPUT")) || (var.equals("OUTPUT"))'
char1 = '("'
char2 = '")'


add = line[line.find(char1)+2 : line.find(char2)]
list.append(add)
line1=line[line.find(char2)+1:]
add = line1[line1.find(char1)+2 : line1.find(char2)]
list.append(add)
print(list)

just add those 3 lines in your code, and you're done

1 Comment

how that method will help if will be more than two ("...") in line?
0

if I understand you correct, than something like that is help you:

line = 'line = if ((var.equals("INPUT")) || (var.equals("OUTPUT"))'
items = []
start = 0
end = 0
c = 0;
while c < len(line):
    if line[c] == '(' and line[c + 1] == '"':
        start = c + 2
    if line[c] == '"' and line[c + 1] == ')':
        end = c
    if start and end:
        items.append(line[start:end])
        start = end = None
    c += 1

print(items)    # ['INPUT', 'OUTPUT']

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.