繁体   English   中英

使用正则表达式从文本中提取完整单词

[英]Using regular expression to extract full words from text

我一直在分析数据,我得到了一个像这样的字符串:

"Scottish Premier League (click here to open|close this coupon)"

我想使用正则表达式提取Scottish匹配组1和Premier League第2组的“苏格兰超级联赛”。

请告诉我使用正则表达式的方法。

MatchCollection matchCol = reg.Matches("Scottish Premier League (click here to open|close this coupon)");

如果您只想匹配每个特定的单词,则您的正则表达式可能类似于:

(Scottish) (Premier League)

如果要匹配第一个单词,则接下来的两个单词:

([\w]+) ([\w]+ [\w]+)

另一种写出单词之间多个空格的方式是:

(\w+)\s+(\w+\s+\w+)

/(苏格兰)(英超联赛)/

基本和直接:

$s =  "Scottish Premier League (click ... coupon)";
$s =~ m/(Scottish) (Premier League)/;
print "Match groups one and two: '$1' '$2'\n";

您可能想要更通用的匹配:

$s =  "Generalized Matching on a string (click ... coupon)";
$s =~ m/^(\S+)\s(.+)\s+\(click/;
print "Match groups one and two: '$1' '$2'\n";

这些是Perl; 下次再具体一点。

此外,请使用RegexBuddyExpresso之类的工具来帮助自己。

鉴于您只给出了要应用正则表达式的一个字符串,因此很难确定该解决方案是否适用于您的其他各种情况:

/^(\w*) (.*) \(/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM