0

Somewhat a continuation of my previous question:

I came across another pattern that I'd have to take care of, which looks something like this:

Tue 01/24/12 1/24/2012 2:56:25 PM

In which case I'd only want it to match the 1/24/2012 2:56:25 PM portion.

My previous expression seems to match the above input on 01/24/12 1 or something similar.

I was able to make this work, for the most part, by using the following expression:

(?:\w\w\w (0?[1-9]|1[012])[- \/.](0?[1-9]|[12][0-9]|3[01])[- \/.](19|20)?\d\d)? (0?[1-9]|1[012])[- \/.](0?[1-9]|[12][0-9]|3[01])[- \/.](19|20)?\d\d((?: |\s*-\s*)(?:(?:([01]?\d|2[0-3]):)?([0-5]?\d):)?([0-5]?\d)( AM| PM)?)?

The issue here is that I don't want to actually include the Tue 01/24/12 bit in my match; I want to make sure that part does not match. I attempted to use a negative look ahead by adding the ?! modifiers to the first non-capturing group, but it didn't quite do what I thought it'd do.

I've tried looking at similar questions here and here, but the answers did not explain anything; they simply provided a working expression for that particular instance.

4
  • Please have a look at regex101.com/r/tR0yT3/1, I think you also want to capture 01/24/12 in your input string. Commented May 5, 2015 at 20:57
  • I'm trying to only capture the date starting at the second date (1/24/2012 in the example), because after I capture it I'm converting it to a .NET datetime object. Commented May 5, 2015 at 21:05
  • Yes, but it is a separate match. Please have another look at regex101.com/r/tR0yT3/2 and the matches. Are you using C#? Please post your code. Commented May 5, 2015 at 21:09
  • Your regex will find 2 matches inside 01/24/12 1/24/2012 2:56:25 PM. Otherwise, please reformulate the requirements. Commented May 5, 2015 at 21:37

1 Answer 1

1

Whenever you are using (...) in your regex, you are creating capture Groups that returns those matches into groups.

In your case, you just need to create a group that contains your desired output, having that in mind i changed your regex a little and group $4 have your desired output:

(?:\w\w\w (0?[1-9]|1[012])[- \/.](0?[1-9]|[12][0-9]|3[01])[- \/.](19|20)?\d\d)? ((0?[1-9]|1[012])[- \/.](0?[1-9]|[12][0-9]|3[01])[- \/.](19|20)?\d\d((?: |\s*-\s*)(?:(?:([01]?\d|2[0-3]):)?([0-5]?\d):)?([0-5]?\d)( AM| PM)?))?

Tested on regexr.com:

enter image description here

To address your spacing matching issue, you need to include the space after the first(...)? group inside second (...)? group (I included as \s?), leaving you with:

(?:\w\w\w (0?[1-9]|1[012])[- \/.](0?[1-9]|[12][0-9]|3[01])[- \/.](19|20)?\d\d)?(\s?(0?[1-9]|1[012])[- \/.](0?[1-9]|[12][0-9]|3[01])[- \/.](19|20)?\d\d((?: |\s*-\s*)(?:(?:([01]?\d|2[0-3]):)?([0-5]?\d):)?([0-5]?\d)( AM| PM)?))

Also last group can't be (...)? anymore otherwise you would match infinity.

And You should also consider changing all your (...) groups to (?:...) if you do not need to capture them, leaving your desired output in $1

Sign up to request clarification or add additional context in comments.

3 Comments

I was wondering if that'd be the best way to do it, but I wasn't quite sure if there was an easier way to just exclude a specific pattern. This one just about works, but it seems for some reason it's matching every space character in the text as well.
what do you mean by matching space characters? Can you give me an example?
I know what the problem is, but trying to think of a solution. The problem is the space after first(...)? group. and the second group is also a (...)? leaving you with a space match after finding first patern

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.