在字符串正则表达式匹配多次 [英] RegEx Match multiple times in string

查看:232
本文介绍了在字符串正则表达式匹配多次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从一个字符串这是间&LT提取值;<和>>。但是,他们可能会发生多次。

I'm trying to extract values from a string which are between << and >>. But they could happen multiple times.

任何人都可以与常规的前pression来匹配这些帮助;

Can anyone help with the regular expression to match these;

this is a test for <<bob>> who like <<books>>
test 2 <<frank>> likes nothing
test 3 <<what>> <<on>> <<earth>> <<this>> <<is>> <<too>> <<much>>.

我当时想的foreach的G​​roupCollection让所有的值。

I then want to foreach the GroupCollection to get all the values.

任何帮助极大的好评。
谢谢你。

Any help greatly received. Thanks.

推荐答案

提前用积极的外观和向后看断言相匹配的尖括号,使用。*?来匹配括号之间的字符最短的序列。通过遍历查找所有值 MatchCollection 匹配返回()方法。

Use a positive look ahead and look behind assertion to match the angle brackets, use .*? to match the shortest possible sequence of characters between those brackets. Find all values by iterating the MatchCollection returned by the Matches() method.

Regex regex = new Regex("(?<=<<).*?(?=>>)");

foreach (Match match in regex.Matches(
    "this is a test for <<bob>> who like <<books>>"))
{
    Console.WriteLine(match.Value);
}

这篇关于在字符串正则表达式匹配多次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆