Earlz
Earlz

Reputation: 63845

Why doesn't the dot symbol work in this Regex?

NET Regex. (with ignore-case)

I want it to match

field_x_12
field_a_ABC
field_r_something

etc

My question is why the . operator doesn't work in this regex:

field_[.]_.*

yet this (equivalent basically) regex does work:

field_[a-z]_.*

Is there something I'm missing about the dot operator . ?

Upvotes: 4

Views: 206

Answers (5)

Toby
Toby

Reputation: 7544

When it's inside a character class, the dot is just a period, not a wildcard.

Upvotes: 0

Jerod Houghtelling
Jerod Houghtelling

Reputation: 4867

You should try field_._.* because inside the [] it is treatd as an acutal dot.

Upvotes: 0

jwsample
jwsample

Reputation: 2411

Inside brackets . is a literal dot and doesn't match any character.

Upvotes: 0

Oded
Oded

Reputation: 499062

Why are you using [.]? The [] denotes a explicit set of characters, so the . character is what the RegEx is looking for.

field_._.*

Should work fine.

See this handy .NET RegEx cheat sheet.

Upvotes: 1

Daniel Vandersluis
Daniel Vandersluis

Reputation: 94183

A . inside a character class ([...]) is a literal dot character. If you want it to act as a wildcard, don't use the brackets.

Upvotes: 7

Related Questions