Carets in Regular Expressions

杀马特。学长 韩版系。学妹 提交于 2019-12-23 06:06:05

问题


Specifically when does ^ mean "match start" and when does it mean "not the following" in regular expressions?

From the Wikipedia article and other references, I've concluded it means the former at the start and the latter when used with brackets, but how does the program handle the case where the caret is at the start and at a bracket? What does, say, ^[b-d]t$ match?


回答1:


^ only means "not the following" when inside and at the start of [], so [^...].

When it's inside [] but not at the start, it means the actual ^ character.

When it's escaped (\^), it also means the actual ^ character.

In all other cases it means start of the string / line (which one is language / setting dependent).

So in short:

  • [^abc] -> not a, b or c
  • [ab^cd] -> a, b, ^ (character), c or d
  • \^ -> a ^ character
  • Anywhere else -> start of string / line.

So ^[b-d]t$ means:

  • Start of line
  • b/c/d character
  • t character
  • End of line



回答2:


I'm answering this question Meaning of caret depending on the context
which was marked an exact duplicate by Wiktor Stribiżew 11/16/2019
of this Carets in Regular Expressions

Going to ignore block comments ? Ok, this ^\s* might be bad because \s can span lines. See if Dot-net supports horizontal whitespace \h if not [^\S\r\n] works also. Can use multi-line inline modifier (?m) (or RegexOptions.Multiline). That changes the meaning of ^ to mean the beginning of line as opposed to beginning of string (the default). So, it ends up being (?m)^\h*(#). The capture group should tell the position. If not, this is just as well (?m)(?<=^\h*)# and the position of the match is the offset.

See this for complete regex info https://docs.microsoft.com/en-us/dotnet/standard/base-types/regular-expression-language-quick-reference

Note that ^\s* will work of course, but it matches a lot of unnecessary cruft that can span lines.



来源:https://stackoverflow.com/questions/58885857/meaning-of-caret-depending-on-the-context

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!