^,$ When to use this symbol in regex?

2018-12-31 07:21发布

    Owner
                      Owner: ABC
                     Address: XYZ            
    Information

This is the pattern I am trying to Match. I want to Match the Details between Owner and Information but excluding the titles using the Regex like,

(?<=(\s*Owner))(.|\n)*?(?=\s*Information)

When I try to append ^ and $ to be more accurate, it is not matching.

(?<=(/^\s*Owner))(.|\n)*?(?=\s*Information/$)

Could you pls help me in this?

标签: c# .net regex
1条回答
公子世无双
2楼-- · 2018-12-31 07:58

You're not giving enough detail. Since your first expression is working I assume you are using Singleline mode.

My next assumption is, this string is only part of a larger string.

^ is matching the start of the string by default

$ is matching the end of the string by default.

Now, since your string contains stuff before and ahead, you need to change this default behaviour:

With the modifier Multiline, ^ matches the start of a row and $ the end of a row. See the documentation for more details.

So, your regex should look something like this:

Regex regx = new Regex("(?<=^(\s*Owner))(.|\n)*?(?=\s*Information$)", RegexOptions.Singleline | RegexOptions.Multiline);
查看更多
登录 后发表回答