user1472672
user1472672

Reputation: 365

Trouble with regex matching last part

I have the following regex:

solo_LL01_[\p{Alnum}|-]+_\d{10}_V\d{4}(1[0-2]|0[1-9])(3[01]|[12][0-9]|0[1-9])(2[0-3]|[01][0-9])([0-5][0-9])(I|C)?(_firsttest)?\.fits$

Matched against:

solo_LL01_eui-fsi-174_0715246200_V201607271145C_firsttest.fits

I would like to make the firsttest text any numbers/characters. I tried some variations such as:

solo_LL01_[\p{Alnum}|-]+_\d{10}_V\d{4}(1[0-2]|0[1-9])(3[01]|[12][0-9]|0[1-9])(2[0-3]|[01][0-9])([0-5][0-9])(I|C)?(_[\p{Alnum}_])?\.fits$

But just seem to get it to work. Many thanks for any help

Upvotes: 0

Views: 55

Answers (2)

Exception_al
Exception_al

Reputation: 1079

Looking at the documentation of Pattern, especially the parts about "Predefined character classes" :

\w A word character: [a-zA-Z_0-9]

and "Quantifiers":

X* : X, zero or more times

X+ : X, one or more times

The following should match any number of charatcter / numbers :-

[\w]*

For one or more Characters or Numbers :-

[\w]+

In fact what you tried is also correct, except that you missed the quantifier * (any number of matches) or + (1 or more matches) ... so your regex just needs to be :-

solo_LL01_[\p{Alnum}|-]+_\d{10}_V\d{4}(1[0-2]|0[1-9])(3[01]|[12][0-9]|0[1-9])(2[0-3]|[01][0-9])([0-5][0-9])(I|C)?(_[\p{Alnum}_]*)?\.fits$

or

solo_LL01_[\p{Alnum}|-]+_\d{10}_V\d{4}(1[0-2]|0[1-9])(3[01]|[12][0-9]|0[1-9])(2[0-3]|[01][0-9])([0-5][0-9])(I|C)?(_[\p{Alnum}_]+)?\.fits$

Depending on which Quantifier (* or +) you wish to use

Upvotes: 1

AutomatedChaos
AutomatedChaos

Reputation: 7500

Why not use [A-Za-z0-9]+?

solo_LL01_[\p{Alnum}|-]+_\d{10}_V\d{4}(1[0-2]|0[1-9])(3[01]|[12][0-9]|0[1-9])(2[0-3]|[01][0-9])([0-5][0-9])(I|C)?_([A-Za-z0-9]+)?\.fits$

Upvotes: 0

Related Questions