正则表达式匹配特定格式 - 一个大写字母但不是两个

时间:2016-03-09 08:48:56

标签: regex lookahead lookbehind negative-lookahead negative-lookbehind

preg_replace

例:

.+(?<![a-z]\s)([A-Z](?=[a-z'-]+)[a-z'-]+)(?!\s).+\((.+)\).+(?<![a-z]\s)([A-Z](?=[a-z'-]+)[a-z'-]+)(?!\s).+\((.+)\)

1和2应匹配,但3和6不匹配。它们在()之前的部分有两个单词。我试图做一个(?!\ s)或(?!\ b)来忽略下一个单词,但它似乎只是回溯到前一个字符并忽略它。

结果

[Nisei](slightly scratched) [Ocellaris](unconcious)
L: 1799 Ocellaris: (slightly scratched) vs. N'isei: (mildly wounded)
[Nisei](slightly scratched) [Zealot Warrior](perfect condition)
L: 1799 Ocellaris: (slightly scratched) vs. zealot warrior: (mildly wounded)
[fire dragon](slightly scratched) [Zealot Warrior](perfect condition)
[King Jheric](slightly scratched) [Zealot Warrior](perfect condition)

更新

一般模式将是

Case 1:
1: [1,6] Nisei
2: [8,26] slightly scratched
3: [29,38] Ocellaris
4: [40,57] unconcious
Case 2:
1: [8,17] Ocellaris
2: [20,38] slightly scratched
3: [44,50] N'isei
4: [53,67] mildly wounded
Case 3:
1: [1,6] Nisei
2: [8,26] slightly scratched
3: [29,34] Zealo
4: [45,62] perfect condition
Case 4:
No Match
Case 5:
No Match
Case 6:
1: [1,4] Kin
2: [14,32] slightly scratched
3: [35,40] Zealo
4: [51,68] perfect condition

人只能拥有一个大写字母,而一个NPC可以有两个不同大写的名字...... King Jheric vs wolfen berserker vs zealot warrior。

它必须模糊的原因是它必须匹配像

这样的模式
Person or NPC (condition) Person or NPC (condition)

更新2:

Me:(condition) v Target:(condition) 
Reply:Some Person L:1200 King Jheric:(condition) vs. Target:(condition)
[Me] -> (condition) [wolfen berserker] -> (condition)
Lag: 1200 [zealot warrior](condition) vs. [King Jheric](condition)

这解决了上面列出的所有案例,包括原件,除了第一个或第二个&#34;事物&#34;有两个单词,其中至少有一个是大写的。

1 个答案:

答案 0 :(得分:1)

根据您的信息,这种模式应该做的工作:

(?<![a-z'-] )([A-Z][a-z'-]++)[^(A-Z]*\(([^)]+)\)[^A-Z\v]+([A-Z][a-z'-]++)(?!\s[A-Z])[^(A-Z]*\(([^)]+)\)
  • (?<![a-z'-] )是一个负面的背后隐藏,以确保不匹配小写字母或&#39;或 - 和一个空间(为邪恶的国王Jhared)
  • ([A-Z][a-z'-]++)匹配一个大写字母,后跟小写字母,撇号,连字符 - possesive,因此引擎不会尝试后退
  • [^(A-Z]*匹配任何不打开括号和大写字母的字符(King Jhared,你还记得) - 也许你可以在这里使用[: ]*,如果你想检查
  • \(([^)]+)\)匹配左括号,一个或多个字符不是右括号,然后是右括号
  • [^A-Z\v]+匹配任何不是大写字母或换行符的字符一次或多次
  • ([A-Z][a-z'-]++)匹配一个大写字母,后跟小写字母,撇号,连字符 - possesive,因此引擎不会尝试后退
  • (?!\s[A-Z])是一个先行者,以确保其后面没有空格和大写字母
  • [^(A-Z]*匹配任何不打开括号和大写字母的字符数
  • \(([^)]+)\)匹配左括号,一个或多个字符不是右括号,然后是右括号

您可以在此处找到包含所有示例的演示:https://regex101.com/r/nB5jP4/2