-1

I want to use regex to find a text between two words. This almost works. But it also returns parts of the words that are searched for.

Result: sub)a(/sub But I need: a Only the letter a. Possibly (sub) and (/sub) can occur several times in the string.

How do I have to change the regex expression? Thanks.

var start = "(sub)";
var end = "(/sub)";
var text = "Border Low P(sub)a(/sub) 12";
var text2 = "Border Low P(sub)a(/sub) and (sub)b(/sub)";

Regex regex = new Regex($"{start}(.+?){end}");
var v = regex.Match(text);

// result: sub)a(/sub
Tommer
  • 25
  • 1

1 Answers1

1

I suggest

(?<=\(sub\)).*?(?=\(\/sub\))

pattern where.

Explanation:

(?<=\(sub\))  - look behind for "(sub)", note escapement \( and \)
.*?           - arbitrary symbols but as few as possible
(?=\(\/sub\)) - look ahead for "(\sub)", note escapement \(, \/ and \)

Code:

var text = "Border Low P(sub)a(/sub) and (sub)b(/sub)";

Regex regex = new Regex(@"(?<=\(sub\)).*?(?=\(\/sub\))");

// Let's have an array of matches
var v = regex
  .Matches(text)
  .Cast<Match>()
  .Select(match => match.Value)
  .ToArray();

// Let's have a look
Console.WriteLine(string.Join(", ", v));

Output:

a, b

Fiddle

Edit:

If you want to build the pattern from given start and end use Regex.Escape, e.g.

var start = "(sub)";
var end = "(/sub)";
        
Regex regex = new Regex(@$"(?<={Regex.Escape(start)}).*?(?={Regex.Escape(end)})");
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215