[英]Using regular expression to extract full words from text
I have been working with parsing data, I got a string like: 我一直在分析数据,我得到了一个像这样的字符串:
"Scottish Premier League (click here to open|close this coupon)"
I would like to extract "Scottish Premier League" with Scottish
Matching Group 1 and Premier League
Matching Group 2, using regular expression. 我想使用正则表达式提取Scottish
匹配组1和Premier League
第2组的“苏格兰超级联赛”。
Please show me the way to do that using regular expression. 请告诉我使用正则表达式的方法。
MatchCollection matchCol = reg.Matches("Scottish Premier League (click here to open|close this coupon)");
If you just want to match each specific word then your regex could be something like: 如果您只想匹配每个特定的单词,则您的正则表达式可能类似于:
(Scottish) (Premier League)
If you want to match the first word then the next two: 如果要匹配第一个单词,则接下来的两个单词:
([\w]+) ([\w]+ [\w]+)
Another way of writing this that accounts for multiple spaces between words is: 另一种写出单词之间多个空格的方式是:
(\w+)\s+(\w+\s+\w+)
/(苏格兰)(英超联赛)/
Basic and direct: 基本和直接:
$s = "Scottish Premier League (click ... coupon)";
$s =~ m/(Scottish) (Premier League)/;
print "Match groups one and two: '$1' '$2'\n";
You probably wanted more generalized matching: 您可能想要更通用的匹配:
$s = "Generalized Matching on a string (click ... coupon)";
$s =~ m/^(\S+)\s(.+)\s+\(click/;
print "Match groups one and two: '$1' '$2'\n";
These are Perl; 这些是Perl; be more specific next time. 下次再具体一点。
Also, help yourself, use a tool, like RegexBuddy or Expresso . 此外,请使用RegexBuddy或Expresso之类的工具来帮助自己。
鉴于您只给出了要应用正则表达式的一个字符串,因此很难确定该解决方案是否适用于您的其他各种情况:
/^(\w*) (.*) \(/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.