0

Can someone help me providing correct regex. I want to remove date from a string.

Ex string : IO_SE_SOURCE_2015-01-30T19_14_59 I want to remove the date part in the string and fetch only IO_SE_SOURCE as the result

I am doing this in python using re.sub() def.

So, when I do the below command

var=re.sub(regexpattern,repl,"POC_LN_SOURCE_2015-01-30T19_14_59.something")

var should contain the value POC_LN_SOURCE

-Thanks

5
  • Do you want to capture everything from the beginning of the string up to _<datetime>? Commented Jul 4, 2017 at 23:05
  • Try re.sub('([A-Z_]+)_([\d\-T_]+)(.+)','\\1', 'POC_LN_SOURCE_2015-01-30T19_14_59.something'). Commented Jul 4, 2017 at 23:10
  • @AlexHall - Yes. I want capture before(_datetime) to a variable Commented Jul 4, 2017 at 23:16
  • @Abdou- I tried. The entire is been captured. I want only POC_LN_SOURCE to be captured. Commented Jul 4, 2017 at 23:18
  • That's exactly what the code captures. It returns POC_LN_SOURCE. Isn't that what you're getting? Commented Jul 4, 2017 at 23:21

1 Answer 1

2

There's no need for re.sub, just capture a group from a match:

>>> re.match(r'(.+)_\d{4}-\d{2}-\d{2}', 'POC_LN_SOURCE_2015-01-30T19_14_59.something').group(1)
'POC_LN_SOURCE'
Sign up to request clarification or add additional context in comments.

Comments

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.