C# 所有大写单词的正则表达式,直到小写
我有这样一句话:C# 所有大写单词的正则表达式,直到小写,c#,regex,C#,Regex,我有这样一句话: Name: JOHN J. SMITH Sometag: 我如何抓住约翰·史密斯的部分 Sometag并不总是相同的,所以它更像是获取所有大写单词,直到其中一个不相同 更新 “[A-Z.]*”返回JOHN J.SMITH S “[A-Z.]*\b”不返回任何内容,也不返回 “\b[A-Z.]*\b”试试这个 [A-Z. ]*\b 让我知道进展如何 你可以用这个更完整 [\p{Lu}\p{M}\p{Z}\p{N}\p{P}\p{S}]*\b 但这是一口 Match a si
Name: JOHN J. SMITH Sometag:
我如何抓住约翰·史密斯的部分
Sometag
并不总是相同的,所以它更像是获取所有大写单词,直到其中一个不相同
更新
“[A-Z.]*”
返回JOHN J.SMITH S
“[A-Z.]*\b”
不返回任何内容,也不返回
“\b[A-Z.]*\b”
试试这个
[A-Z. ]*\b
让我知道进展如何
你可以用这个更完整
[\p{Lu}\p{M}\p{Z}\p{N}\p{P}\p{S}]*\b
但这是一口
Match a single character present in the list below «[\p{Lu}\p{M}\p{Z}\p{N}\p{P}\p{S}]*»
Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
A character with the Unicode property “uppercase letter” (an uppercase letter that has a lowercase variant) «\p{Lu}»
A character with the Unicode property “mark” (a character intended to be combined with another character (e.g. accents, umlauts, enclosing boxes, etc.)) «\p{M}»
A character with the Unicode property “separator” (any kind of whitespace or invisible separator) «\p{Z}»
A character with the Unicode property “number” (any kind of numeric character in any script) «\p{N}»
A character with the Unicode property “punctuation” (any kind of punctuation character) «\p{P}»
A character with the Unicode property “symbol” (math symbols, currency signs, dingbats, box-drawing characters, etc.) «\p{S}»
Assert position at a word boundary «\b»
或更短
\P{Ll}*\b
更新1
在你编辑之后,我会用这个
Name: (\P{Ll}*)[ ]
所需匹配项将在第1组中。请注意,我在最后添加了一个[],以表示单个空格。如果需要,可以将此字符类转换为空格
在C#中,这变成了
string resultString = null;
try {
Regex regexObj = new Regex(@"Name: (\p{Ll}*)[ ]");
resultString = regexObj.Match(subjectString).Groups[1].Value;
} catch (ArgumentException ex) {
// Syntax error in the regular expression
}
不能使用否定的向前看,并发现大写字母后面没有小写字母
(([A-Z.])(?![A-Z:]))+
String caps=Regex.Match(“名称:JOHN J.SMITH Sometag:”,“([A-Z.])(?![A-Z:])+”)。ToString()
@Neal:有一次,我给一位同事发了一份他们做错事情的清单。我现在对这一决定感到遗憾。答案是什么都没有。我很少使用正则表达式,所以我不知道。@TomFobear如果您提供更新1的正则表达式,则输入“Name:JOHN J.SMITH Sometag:”组1中将包含“JOHN J.SMITH”。这就是你需要的不?