sigil sigil - 3 months ago 45
C# Question

RegexOptions.Multiline seems to ignore \n

I'm trying to search a string using Regex. Here's what the string looks like in the Text Visualizer:

0 -12.67 Td
/Helv 14 Tf
(Source: ABC / XYZA) Tj
0 -15.624 Td
(Job Source No.: GRQX ID 27299) Tj
0 -15.624 Td

When I view the value by hovering on it:

0 -12.67 Td\n/Helv 14 Tf\n(Source: ABC / XYZA) Tj\n0 -15.624 Td\n(Job Source No.: GRQX ID 27299) Tj\n0 -15.624 Td

I'm using
with the following pattern and


This returns no matches. When I omit the caret, like this:


returns both matches:

Source: ABC / XYZA
Job Source No.: GRQX ID 27299

How can I match on the first character in a line?


A lookbehind in a regex pattern checks the characters to the left of the current position, so in your pattern ^(?<=[(]) requires a ( to be before the ^ (the start of a line). Before a start of a line, there is either nothing (at the start of a string), or there is a newline char. Thus, it will never match any string.

Actually, you do not need any lookarounds to get the substrings you need. Use the following regex with RegexOptions.Multiline option:


The ^ will make sure the match appears at the start of a string, ([^()]+) will capture into Group 1 one or more chars other than ( and ), and then ) will get matched.

See the regex demo, the results you need are in Group 1.

enter image description here

In C#, use the following code:

var res = Regex.Matches(str, @"^\(([^()]+)\)", RegexOptions.Multiline)
    .Select(m => m.Groups[1].Value)