Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 用于从段落中仅获取标题大小写中的单词的正则表达式代码_Regex - Fatal编程技术网

Regex 用于从段落中仅获取标题大小写中的单词的正则表达式代码

Regex 用于从段落中仅获取标题大小写中的单词的正则表达式代码,regex,Regex,我正在寻找一个正则表达式,它只返回给定句子或段落中标题大小写中的单词(其中只有第一个字母大写) 如果该段是: France’s last serious attempt at ambitious economic reform, an overhaul of pensions and social security, was in the mid-1990s under President Jacques Chirac. 我想把它和法国、总统雅克和希拉克相配 (我用Python 3编写)根据正

我正在寻找一个正则表达式,它只返回给定句子或段落中标题大小写中的单词(其中只有第一个字母大写)

如果该段是:

France’s last serious attempt at ambitious economic reform, an overhaul of pensions and social security, was in the mid-1990s under President Jacques Chirac.
我想把它和法国、总统雅克和希拉克相配


(我用Python 3编写)

根据正则表达式的风格,结果可能会有所不同

对于PCRE,我建议:

/\b[A-Z][a-z]*\b/

使用单词边界、大写字母,然后使用尽可能多的小写字母,如下所示:

\b[A-Z][a-z]+
像这样:

titleWords = re.findall(r"\b[A-Z][a-z]+", line)

请注意,
+
(至少1)比
*
(0或更多)更可取,因此您不匹配单个大写字母,例如
“I”
“A”


单词边界实际上是不必要的,但会阻止匹配camelcase单词,如
“mySpace”
,而这些单词无论如何都不应该出现在常规文本中,因此您可以删除
\b
,而不会产生不良影响。

要处理任何语言字母,请使用unicode属性:

re.findall(r"\b\p{Lu}\p{Ll}+", inputLine)
在哪里

  • \p{Lu}
    代表任何语言中的任何大写字母
  • \p{Ll}
    代表任何语言中的任何小写字母

法国的<代码>法国的怎么样?刚刚检查过,它可以工作
\b
是一个“单词边框,
似乎被认为是我测试过的,它似乎只与第一个单词匹配。我做错什么了吗?丢掉括号:你不需要它们。整场比赛是最后一场target@john您需要将
g
添加到末尾(“全局”标志)以匹配所有出现的情况,即
/\b[A-Z][A-Z]+//g
仅限Unicode或ASCII?您正在使用哪种语言/工具?使用Python 3、UTF-8。