Question

Hello i`m trying to create a regex that will match all words between two words. my work so far is:

 public static String GetTextBetween(String source, String leftWord, String rightWord)
    {
        return
            Regex.Match(source, String.Format(@"{0}\b\s(?<words>[\w\s]+)\s{1}", leftWord, rightWord),
            RegexOptions.IgnoreCase).Groups["words"].Value;

    }

the text document contains the following:

TYP=123456789_AD1

DES=xyzer

TXT=bXper xyer

TXT=c

Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes

TXT=dsome text

TXT=e some text

TXT=i

as you can see, sometimes the text is right after the words (leftWord and rightWord) and I`m struggling with this one.

Was it helpful?

Solution

The problem is the '\b' which is a word boundary. This will only match after the last character of a string if the last character in that string is a 'word' character. Since the '='is not a word character the match is failing.

@"{0}\s(?<words>[\w\s]+)\s{2}"

Removing the \b should work just fine, or if you need to keep it for some reason use [\b\s] .

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top