我有一句话:
Name: JOHN J. SMITH Sometag:
我如何抓住JOHN J SMITH
部分?
Sometag
并不总是一样的,所以更像是获得所有全部大写的词,直到不是。
更新
"[A-Z. ]*"
返回JOHN J. SMITH S
"[A-Z. ]*\b"
只返回任何内容
"\b[A-Z. ]*\b"
答案 0 :(得分:3)
试试这个
[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
}
答案 1 :(得分:0)
无法使用负面预测并发现大写字母后面没有小写字母?
(([A-Z。])(?![a-z:]))+
String caps=Regex.Match("Name: JOHN J. SMITH Sometag: ","(([A-Z. ])(?![a-z:]))+").ToString()