RegEx ignores part of a string to extract text
I have the following line:
@delimabc@delim@delim123@delim@delim456@delim
and I need to write a .NET RegEx that finds 3 matches in this example (but suppose the number of matches is variable:
- ABC
- 123
- 456
How can I write a RegEx so that the expression only matches the first and second @delim and then the third and fourth, etc.?
Below, of course, will be written from the first to the last instance of the @delim line.
@delim(.+)+@delim
source to share
You can use look and feel like:
(?<=@delim)\w+
(?<=@delim)
uses Positive Lookbehind which will match characters @delim
literally (case sensitive)
while \w+
will match any character in the word from [a-zA-Z0-9_]
. To include or exclude characters, you can replace \w
with [a-zA-Z0-9_]
and include new characters, or remove those that should not be evaluated in your expression.
Here is a .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|$)
source to share
You can use split on this regex:
(?:@delim)+
Demo version of RegEx
Alternatively, replace the given regex pattern with an empty string .
source to share