简体   繁体   中英

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 Regex.Matches() with the following pattern and RegexOptions.Multiline :

^(?<=[(]).+(?=[)])

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

(?<=[(]).+(?=[)])

then Regex.Matches() 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.

在此处输入图片说明

In C#, use the following code:

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

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM