【问题标题】:Match the substring that starts from the last capital letter word that is not in parenthesis to the end of the string匹配从最后一个不在括号中的大写字母单词开始到字符串末尾的子字符串
【发布时间】:2015-07-06 16:42:03
【问题描述】:

我需要与最后一个前导(单词中的第一个)大写字母匹配的子字符串,该大写字母不在括号内到字符串的末尾

我尝试使用这个正则表达式:

([A-Z][a-zA-Z\-\']*\s*)*(\s+\([^)]*\)[\s]*)+$

匹配这个输入:

can This Be PosSible (Ignore aNy Upper CAse in parenthesis)

匹配结果是:

This Be PosSible (Ignore aNy Upper CAse in parenthesis)

但所需的输出是:

PosSible (Ignore aNy Upper CAse in parenthesis)

this on regex101

【问题讨论】:

  • @ndn 我想要从字符串末尾开始的第一个大写字母单词
  • 感谢@JoopEggen,这行得通

标签: java regex string


【解决方案1】:

这似乎适用于您的示例:

\b[A-Z](?![^(]*\))(?!.*\b[A-Z](?![^(]*\))).*

live demo

【讨论】:

  • 如果最后一个单词中有大写字母,这不起作用。尝试匹配这个“我们不能这样做,因为”。匹配是“Ause”,应该是“BecAuse”。你能推荐一个替代方案吗
  • @lederp 当然,很简单 - 添加单词边界。查看编辑和新的演示链接。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-25
  • 2022-07-06
  • 1970-01-01
  • 2021-09-14
  • 1970-01-01
相关资源
最近更新 更多