简体   繁体   English

正则表达式基本帮助

[英]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] 我正在尝试创建一个正则表达式来检查字符串的最后2个字符为[空格] [anychar]

I found a tutorial online which says to match A to the end of the string you do 我在网上找到了一个教程,说要将A匹配到您要完成的字符串的末尾

 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!). 它使您不必担心非英语字母( AZ是非常有限的字母!)。

(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.) (PS您也可以使用char.IsWhiteSpace而不是直接与' '进行比较;然后它也可以与其他空格字符一起使用。例如,亚洲用户可能会输入U + 3000表意文字空间,而不是标准U + 0020空间)

Ditch the brackets and do it like that: \\s[A-Za-z]$ . 抛开括号,像这样: \\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 . \\s代表“任何空格字符”, [A-Za-z]代表“此子集中的任何字符:A-Za-z。“ AZ”类似于“ A to Z diapason”的关键字,但通常您可以使用方括号来表示类似“这些符号中的任何一个”之类的词,例如,模式[so]将匹配任何so的字母。

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 , ! 因此[^so]将匹配ab ,! and all other symbols but won't match s or o . 和所有其他符号,但不匹配so

EDIT: if you're trying to match an initial, "AZ" might not be the best idea. 编辑:如果您想匹配一个初始,“ AZ”可能不是最好的主意。 Use the unicode \\p{L} property. 使用unicode \\p{L}属性。

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 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 ($)'. 等于“在字符串($)的末尾完全匹配一个破折号空格和集合AZ或az中的一个字符”。

To test lastname,firstname middlename or lastname,firstname you would use quantifiers to say 'how many of what should be matched': 要测试lastname,firstname middlenamelastname,firstname您可以使用量词来说明“应匹配多少个”:

/^.+,.+\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 ($)'. 等于'从字符串(^)的开头,匹配任意字符(。),1次或多次(+),后跟一个逗号,再跟任意字符一次或多次,后跟零或一个(?)空格距离字符串($)末尾零或多次(*)的任何字符”。

Use this as a starting point and build in any required complexity. 以此为起点,构建任何所需的复杂性。

These regex should do the trick. 这些正则表达式可以解决问题。 \\w+,\\w+(?:\\s\\w+)?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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