Isaac85
Isaac85

Reputation: 53

I want REGEX to match dots between each letter C#

I have Initials field and I want to be sure that the inputted format is like:
x.x. ( capital or small letter )
For example:
the following would be valid: F.H.
the following would be invalid: FH OR F.H OR FH.

I have used these regex forms:

\.([a-zA-Z])+\.
([a-zA-Z]\.)+

but it did not work for every letter!

Thanks in advance!

Upvotes: 2

Views: 68

Answers (1)

lime
lime

Reputation: 7111

In your examples, you mention having used ([a-zA-Z]\.)+. That regex should match F.H.

Depending on the code you're using, you likely need to ensure that you're matching against the entire line or string by including the ^$ or \A\z metacharacters. In that case you would have ^([a-zA-Z]\.)+$ or \A([a-zA-Z]\.)+\z, and that would ensure that F.H or FH. aren't accidentally matched.

On top of that, you may want to take into account that not all of your users have ASCII-only names. You could then use the \p{L} or \p{Letter} Unicode category to include letters from all languages.

Putting it all together, I would suggest the following regex:

^(\p{L}\.)+$

As you can see in this fiddle, that will match the example strings F.H., A.B.C. & Ö.É. while not matching FH, F.H, FH. or A.5.

Upvotes: 2

Related Questions