正则表达式中的插入符号 [英] Carets in Regular Expressions

查看:51
本文介绍了正则表达式中的插入符号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

具体来说,^ 在正则表达式中什么时候表示匹配开始",什么时候表示不是以下"?

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

从维基百科的文章和其他参考资料中,我得出结论,它意味着前者在开头,后者在与括号一起使用时,但是程序如何处理插入符号在开头和括号中的情况?^[b-d]t$ 匹配什么?

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?

推荐答案

^ 仅在 [] 的内部和开头表示不是以下",所以[^...].

^ 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).

简而言之:

  • [^abc] -> 不是 a、b 或 c
  • [ab^cd] -> a、b、^(字符)、c 或 d
  • \^ -> 一个 ^ 字符
  • 其他任何地方 -> 字符串/行的开头.
  • [^abc] -> not a, b or c
  • [ab^cd] -> a, b, ^ (character), c or d
  • \^ -> a ^ character
  • Anywhere else -> start of string / line.

所以 ^[b-d]t$ 的意思是:

  • 行首
  • b/c/d 字符
  • t 字符
  • 行尾

这篇关于正则表达式中的插入符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆