Regex to match alphabets and other characters but not digits c#

Mandviwala, Abdeali 21 Reputation points
2022-08-30T06:10:24.343+00:00

Hello,

I have a string as below in C# :

subject:practitioner.address-state

I have written the regex "^([A-Za-z]+)(:[A-Za-z]+)?.([A-Za-z]+)$" to match the above string but it fails as "address-state" has hyphen (-) in it.

The rest of the part before address-state works fine. Now the last part i.e address-state would have alphabets and/or other characters such as (-,/. etc). It cannot have any numeric value.

I need to modify the above regex to match this.

Regards,
Abdi

Developer technologies | C#
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Viorel 122.6K Reputation points
    2022-08-30T08:24:15.643+00:00

    Try a new expression:

    ^(\p{L}+)(:\p{L}+)?\.(\p{L}+)(-(\p{L}+))*$

    But, if you dislike non-Latin letters, you can use [A-Za-z] instead of \p{L}.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.