I'm trying to extract any strings with a "_" in the middle.
For example, with name1_name2 _ nothing test1 _ test 2_ _3
I would like to extract name1_name2
Thank you for reading!
CodePudding user response:
>>> import re
>>> s = 'name1_name2 _ nothing test1 _ test 2_ _3 name3_name4'
>>> re.findall('[a-zA-Z0-9] _[a-zA-Z0-9] ', s)
['name1_name2', 'name3_name4']
>>> re.findall('\w _\w ', s)
['name1_name2', 'name3_name4']
CodePudding user response:
How's this?
[\w] _[\w]
Does that work for you?
CodePudding user response:
You can use regex given below and it will select any string having single _
in its name.
^\S [_]\S