I have a string which could be of below of format
dir1/abc001_config.xml
cd23-config.xml
dir1/dir2/efg45_config.xml
dir1/dir2/efg45-config.xml
I need to extract a string which has number attached to it. For eg for above it will be
abc001
cd23
efg45
efg45
Please help with regex. I need to run this in shell.
Update:
I looked up for answer but could only find the regex which gives the number in a string.
grep -Eo \d
I understand we need a pattern to be matched,
pattern is matched string can be followed by '-' or ''
and it can have '/', '', '-' just before it.
CodePudding user response:
you can use this regex
(?:(. \/)|^)(. )(?:(-|_). )$
and to extract your results: $2
For explanation, you can translate this regex by: (my_dir or NULL)(my_searched_string)(- or _ followed by a string)
CodePudding user response:
If regex is not necesary try with:
str_has_num = any(char.isdigit() for char in input_str) #returns boolean if input_str has a number