M4I3X
M4I3X

Reputation: 43

Find multiple elements in string in Python

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.

Upvotes: 1

Views: 657

Answers (4)

Raniz
Raniz

Reputation: 11113

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']

Upvotes: 2

Andrey Topoleov
Andrey Topoleov

Reputation: 1919

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']

Upvotes: 0

Samuel GIFFARD
Samuel GIFFARD

Reputation: 842

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 *.

Upvotes: 6

Sandesh34
Sandesh34

Reputation: 280

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

Upvotes: 0

Related Questions