0

I need to select a certain part of a string, that is between two substrings.

A bunch of different regex combinations. I've been stuck on it for two days

\\srv00\DATA\Reports\2019-08-14 09_41_24<Text that can contain any character and needs to be found with regex>.txt (the <> are not actually part of the text)

(The text is not actually between brackets)

I'm using https://regex101.com/ to test my regex. Using the following:

(?:[\s\S]*[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}_[0-9]{2}_[0-9]{2} )(.*)(?=\.txt)

I was able to get it correct in group, but not in the full match, resulting in everything in front of .txt to be printed.

Note that the everything in front of the date and time can also be variable.

I want to select just the substring, But I'm getting the entire string in result.

  • The regex looks like it's working on regex101, am I correct? Does the issue come when you use the regex from another programming language? – Rob Streeting Aug 14 '19 at 08:50
  • What language are you using? Your regex looks good to me, try doing `match` and `group(1)`on the result if you are using Python. – palvarez Aug 14 '19 at 08:53
  • I'm pretty new to regex, I didn't know how groups and match worked. Figured it out now. – BlaxadowFire Aug 28 '19 at 11:27

1 Answers1

2

You could double check your regex also using txt2tre, I find this page quite helpful for creating complex regex.

If you get the proper groups and you use Python's re module, you can call the matched groups in a string m; e.g.

m.group(0) := complete string
m.group(1) := the first matched group etc.
palvarez
  • 1,508
  • 2
  • 8
  • 18
cptnJ
  • 230
  • 2
  • 8