^,$何时使用正则表达式这个符号? [英] ^,$ When to use this symbol in regex?
问题描述
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?
推荐答案
你没有给予足够的细节。由于您的第一八佰伴pression正在我假设你正在使用单线
模式。
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);
这篇关于^,$何时使用正则表达式这个符号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!