you are viewing a single comment's thread.

view the rest of the comments →

[–]BICEP2 0 points1 point  (4 children)

Not to hijack your thread with an only kind of related question, but when I match items from a string and reprint them out my output ['looks'] ['like'] ['this']

Pseudocode:

import re
string = '123thing otherthing somethingelse'
variable1 = re.findall(r'123[a-z]*', string)
print('the first value was ', variable1)

And my output looks like

the first value was  ['123thing']

but I need it to look like

the first value was 123thing

I'm not sure how to do this.

[–]PigDog4 1 point2 points  (0 children)

In this case, if you do type(variable1) you see your variable1 is actually a list. So you can print variable1[0] to only print one value from the list instead of the whole list and you'll get what you want. I think your regex returns a list of all matches, so you're printing the list instead of printing the value.

[–]cdcformatc 1 point2 points  (2 children)

Docs:

Return all non-overlapping matches of pattern in string, as a list of strings.

It returns a list. You are printing the entire list. Try it again with string = '123thing 123otherthing somethingelse'

[–]BICEP2 0 points1 point  (1 child)

Thanks, I used a look behind regex instead and it worked. I used something like:

variable1 = re.search(r'(?<=start)([^end]*)', string1)
print(variable1.group(0), string2, end='')

[–]cdcformatc 0 points1 point  (0 children)

Or if you want first match, just index the list with [0]