[Solved] How to Write Regular Expression for Indian Names [closed]


If you want to catch all strings containing only upper and lowercase characters, periods and spaces, you can use

^[a-zA-Z\. ]+$

Here’s a breakdown of how the regex works

^ matches the beginning of the string

[a-zA-Z\. ] matches any character a-z, A-Z, or a period or space
    + makes the above match any string with 1 or more characters

$ matches the end of the string

You can test this regex here, at regex101.com.

The regex could probably be created better, though, to ensure that the string does not end or start with a period or a space

^(?![\. ])[a-zA-Z\. ]+(?<! )$

This is essentially the same as the above regex, except it uses negative lookaheads and negative lookbehinds to make sure the string does not start with a period or space or end with a space

( Beginning of the group

    ?! makes the group a Negative Lookahead

     [\. ] Matches either a period or space (in this case, because we are looking a negative lookahead, it will make sure it does not match this)

) End of the group

( Beginning of the second group

    ?<! makes the group a Negative Lookbehind

     The space matches a literal space (in this case, because we are looking a negative lookbehind, it will make sure it does not match this)

) End of the second group

You can test this regex here on regex101.com

7

solved How to Write Regular Expression for Indian Names [closed]