0

I written regex for the particular string as mentioned below. match group(1) is including the values. I would need only the string portion rather than values.

import re

line = "dev_interface_values, 123 23, 8 1"
line2= "my_port_values2, 1234, 81"
m = re.search(r'^\s*(.+), (.*\S)\s*$', line)

print  m.group(1)
print  m.group(2)

m1 = re.search(r'^\s*(.+), (.*\S)\s*$', line2)

print  m1.group(1)
print  m1.group(2)

Output:

dev_interface_values, 123 23
8 1
my_port_values2, 1234
81

Expecting Output as :

m.group(1) -> dev_interface_values
m.group(2) -> 123 23, 8 1

m.group(1) -> my_port_values2
m.group(2) -> 1234, 81

2 Answers 2

1

You just need to make first capture group lazy by using ?:

import re
line = "dev_interface_values, 123 23, 8 1"
line2 = "my_port_values2, 1234, 81"
for i in [line,line2]:
    m = re.fullmatch(r'^\s*(.+?), (.*\S)\s*$', i)
    print(m.group(1))
    print(m.group(2))
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot for best answer.
0

Try this regex (see it in action here):

^(\w+)\,\s*(.*\S)$

String 1 output:

dev_interface_values
123 23, 8 1

String 2 output:

my_port_values2
1234, 81

3 Comments

To the downvoter, could you kindly elaborate in what way my answer could be improved?
The above answer is working fine. Thanks a lot. But i don't understood why it is down voted. Any best answers are appreciated.
Thanks, @john517501! I'm not sure what the issue is either - hopefully it can be elaborated upon by the downvoter.

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.