I want to be able to capture everything between sets of ##
. For example the strings:
##First## the dog ate, ##second## the dog ran
=> ['First', 'second']
The dog ##ran##
=> ['ran']
##The## dog ran away from ##home##
=> ['The', 'home']
I tried (?<=##).+?(?=#)
but it captures the first string.
I am using C#
Because of the look behind and look ahead you are going to capture the string in-between first
and second
in ##First## the dog ate, ##second## the dog ran
To correct that, you need to consume them:
(?:##)(?<text>.+?)(?:##)
Then when you match, you can use the group text
to extract the part you want.
For example:
var str = "##First## the dog ate, ##second## the dog ran";
var reg = new Regex("(?:##)(?<text>.+?)(?:##)");
var result = reg.Matches(str).Cast<Match>().Select(m => m.Groups["text"].Value);
result
will contain First
and second
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.