我有这样一句话:
Name: JOHN J. SMITH Sometag:
我将如何抓住JOHN J SMITH
零件?
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
}
不能使用否定的向前看并找到不跟在小写字母后面的大写字母吗?
(([AZ.])(?![az:]))+
String caps=Regex.Match("Name: JOHN J. SMITH Sometag: ","(([A-Z. ])(?![a-z:]))+").ToString()