简体   繁体   中英

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.

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; be more specific next time.

Also, help yourself, use a tool, like RegexBuddy or Expresso .

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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