Rein
Rein

Reputation: 911

Exclude a specific pattern from Regex match

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.

Upvotes: 0

Views: 141

Answers (1)

Rodrigo López
Rodrigo López

Reputation: 4259

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

Upvotes: 1

Related Questions