rik
rik

Reputation: 121

Basic regex help

I am trying to match name patters. A name can either be

lastname,firstname middleinitial

or

lastname,firstname

i am trying to create a regex to check the last 2 chars of a string are [space][anychar]

I found a tutorial online which says to match A to the end of the string you do

 A$

"A" at the end of a line

In applying this to mine i was trying to do something like this, and a number of forms of this too. I literally have no idea though :/

([\\s][A-Za-z]$) 

Upvotes: 1

Views: 128

Answers (5)

MaxMuen
MaxMuen

Reputation: 9

These regex should do the trick. \w+,\w+(?:\s\w+)?

Upvotes: 0

Dyppl
Dyppl

Reputation: 12381

Ditch the brackets and do it like that: \s[A-Za-z]$.

\s stands for "any space character", [A-Za-z] stands for "any character from this subset: A-Za-z. "A-Z" is something like a keyword for "A to Z diapason", but commonly you use brackets to say something like "any of these symbols". For example, the pattern [so] will match any letter which is either s or o.

You can also do it in reverse by adding ^ symbol after the opening bracket so the pattern matches any character that does not occurs in brackets. So [^so] will match a, b, ! and all other symbols but won't match s or o.

EDIT: if you're trying to match an initial, "A-Z" might not be the best idea. Use the unicode \p{L} property.

Upvotes: 1

Timwi
Timwi

Reputation: 66573

You can easily check the last two characters without a regular expression.

bool hasMiddleInitial = false;
if (name.Length > 1 &&
    name[name.Length-2] == ' ' &&
    char.IsLetter(name, name.Length-1))
{
    hasMiddleInitial = true;
}

This is both clearer (more readable) and also executes faster than a regular expression. And it keeps you from having to worry about non-English letters (A-Z is a very limited set!).

(P.S. You could also use char.IsWhiteSpace instead of directly comparing to ' '; then it would work with other space characters too. For example, Asian users are likely to enter a U+3000 ideographic space instead of the standard U+0020 space.)

Upvotes: 4

dianovich
dianovich

Reputation: 2287

This

/\s[A-Za-z]$/

equates to 'match exactly one breaking space and exactly one character from the set A-Z or a-z at the end of the string ($)'.

To test lastname,firstname middlename or lastname,firstname you would use quantifiers to say 'how many of what should be matched':

/^.+,.+\s?.*$/

which equates to 'From start of string (^), match any character (.), 1 or more times (+) followed by exactly one comma, followed by any character one or more times followed by zero or one (?) spaces followed by any character zero or more times(*) to end of string ($)'.

Use this as a starting point and build in any required complexity.

Upvotes: 0

F.B. ten Kate
F.B. ten Kate

Reputation: 2032

The regex you posted will match any two character string that has a space and a letter.

Meaning:

A

etc

I'm not sure what you are exactly trying to match so it's hard to comment on what it should be, i can advise you to try a regex development tool to make your life a bit easier.

http://www.ultrapico.com/Expresso.htm

is one i use (since it's free) but there are a ton out there.

Upvotes: 0

Related Questions