简体   繁体   中英

C# Regex to match any character next to a specified substring but ignoring newline/tab character

I want to match the character next to the word "Test" but if that next character is a newline \\n character I need to get the character next to the newline charactger instead. In the following input string my desired output is character C and w . But I'm getting \\n and w instead:

string str = "This abcTest\nCde and qrvTestwest is an input";
foreach (Match mt in Regex.Matches(str, @"(?<=Test)(.)",RegexOptions.Singleline))
    Console.WriteLine(mt.Groups[1].Value);

Try this:

Test[\n]*(.)

It will skip over any number of newlines.

The regex for what you're asking is:

Test[\n](.)|Test(.)

You need to check both cases Test\\n and Test(.).

Check a working regex of this: https://regex101.com/r/mI2tE5/1

For the comments this works better:

Test[\n]*(.)

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