简体   繁体   中英

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]$) 

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 ( AZ is a very limited set!).

(PS 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.)

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. "AZ" 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, "AZ" might not be the best idea. Use the unicode \\p{L} property.

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.

This

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

equates to 'match exactly one breaking space and exactly one character from the set AZ or az 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM