I have the following string:
@delimabc@delim@delim123@delim@delim456@delim
and I need to write a .Net RegEx that finds 3 matches in this example (but assume the number of matches will be variable:
How can I write a RegEx so that the expression only matches the first and second @delim, and then the third and fourth and so on?
The following will of course capture from the first to the last instance of the @delim string.
@delim(.+)+@delim
You could use look behind like:
(?<=@delim)\w+
(?<=@delim)
is using a Positive Lookbehind which will match the characters @delim
literally (case sensitive)
while \\w+
will match any word character from [a-zA-Z0-9_]
. To include or exclude characters you could replace \\w
by [a-zA-Z0-9_]
and include the new characters or remove those that should not be evaluated in your expression.
Here is .NET Online Demo:
VB.NET version
Dim sampleInput="@delimabc@delim@delim123@delim@delim456@delim"
Dim results = Regex.Matches(sampleInput,"(?<=@delim)\w+")
For Each item As Group In results
Console.WriteLine("Line: {0}", item)
Next
C# Version
var sampleInput = "@delimabc@delim@delim123@delim@delim456@delim";
var results = Regex.Matches(sampleInput, "(?<=@delim)\\w+");
foreach (Group item in results) {
Console.WriteLine("Line: {0}", item);
}
Updated version:
(?<=@delim)[^@].+?(?=@delim|$)
@delim(.+?)@delim
Try this .Set g
flag.Just modifed your regex to add ?
.Grab the caotures.See demo.
You can use split on this regex:
(?:@delim)+
Alternatively replace given regex pattern by an empty string .
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.