Reputation: 281
I have a regular expression as defined
AAA_BBB_CCCC_(.*)_[0-9][0-9][0-9][0-9][0-1][0-9][0-3][0-9]T[0-2][0-9][0-5][0-9][0-5][0-9]
.
There is a string defined as --> **AAA_BBB_CCCC_DDD_EEEE_19710101T123456**
and in the code, we have matcher.group(1) which can filter out what is desired as (DDD_EEEE). Now, I've a new string coming in as --> **AAA_BBB_ATCCCC_DDD_EEEE_19710101T123456**
. Is there a way that I can change the regex to satisfy both old and new string? I tried few solutions that came up from Stackoverflow questions like this and others but that didn't work quite right for me.
Upvotes: 1
Views: 86
Reputation: 627409
You just need to add an optional group, (?:AT)?
, before CCCC
:
AAA_BBB_(?:AT)?CCCC_(.*)_[0-9]{4}[0-1][0-9][0-3][0-9]T[0-2][0-9][0-5][0-9][0-5][0-9]
^^^^^^^
See the regex demo
I also contracted the four [0-9]
to [0-9]{4}
to make the pattern shorter.
The (?:AT)?
is a non-capturing group to which a ?
quantifier is applied. The ?
quantifier makes the whole sequence of letters match 1 or 0 times, making it optional in the end.
Upvotes: 1
Reputation: 704
Please give the following regex a try.
AAA_BBB_(ATCCCC|CCCC)_(.*)_[0-9][0-9][0-9][0-9][0-1][0-9][0-3][0-9]T[0-2][0-9][0-5][0-9][0-5][0-9].
It would only match ATCCCC or CCCC. It won't be able to support dynamic characters preceding CCCC. You would need to use wildcards for that.
Also, you would need to change your matcher.group(1)
statement to matcher.group(2)
Upvotes: 0