-1

I want to search for a specific string can anyone tell me why I am seeing below result? I checked it out in an Online regex site, It seems I have seperated in to 3 groups and now the result is printing the 3 groups. how I can only seperate the first group?

Also is it possible to change the code so the "String" with lower case would be detected?

Relative String

DD-JSH-String43423213-3774
DE-String43423214-SDC-3721

Output:

'String43423213', 'String', '43423213','String43423214', 'String', '43423214'

Code:

matches = re.findall(r'((String)(\d+))', inp)
matches = [j for sub in matches for j in sub if j != ""]

Expected Result:

'String43423213', 'String43423214'
Wiktor Stribiżew
  • 484,719
  • 26
  • 302
  • 397
Sara Daniel
  • 147
  • 10

3 Answers3

1

This is because you are even grouping on the two matches, so you have to remove the outer group. And also you can add flag re.I to ignore case:

matches = re.findall(r'(String)(\d+)', inp, flags=re.I)
print(*[''.join(x) for x in matches],sep="\n")
programmer365
  • 12,641
  • 3
  • 7
  • 28
0

Try this regex-demo:

python source:

input="""DD-JSH-String43423213-3774
DE-String43423214-SDC-3721"""
matches = re.findall(r'String\d+', input, flags=re.I)
print(matches)

or

matches = re.findall(r'(?i)String\d+', input)
print(matches)

output:

['String43423213', 'String43423214']

explanation: Because your regex has two groups String and \d+, re.findall returns a list that contains all tuples of the two groups like ('String', 'String43423214'). You could group it like (String\d+) or non-group like String\d+, both expressions are working.

Heo
  • 201
  • 1
  • 10
-2

you can do this:

import re
inp = """
DD-JSH-String43423213-3774
DE-String43423214-SDC-3721
"""
matches = matches = re.findall(r'String\d+', inp)
for match in matches:
    print(match)