锚定正则表达式和非锚定正则表达式有什么区别? [英] What is the difference between an anchored regex and an un-anchored regex?

查看:90
本文介绍了锚定正则表达式和非锚定正则表达式有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

锚定的正则表达式和非锚定的正则表达式有什么区别?

What is the difference between an anchored regex and an un-anchored regex?

用法在此处找到:

... 这些应该被指定为一个对的列表,其中第一个元素是一个非锚定的正则表达式(在 java.util.regex.Pattern 语法中)平台名称与之匹配的...

... These should be specified as a list of pairs where the first element is an un-anchored regex (in java.util.regex.Pattern syntax) against which the platform name is matched...

推荐答案

Unanchored regex 表示没有锚点的正则表达式模式,^ 表示字符串开头,$ 用于字符串的结尾,因此允许部分匹配.例如.在 Java 中,Matcher#find() 方法可以搜索输入字符串中的部分匹配项,"ac" 将在 "1.0 abc.".锚定的 "^ac$" 模式将匹配 "abc" 字符串,但不会在 "1.0 abc." 中找到匹配项..

Unanchored regex means a regex pattern that has no anchors, ^ for start of string, and $ for the end of string, and thus allows partial matches. E.g. in Java, Matcher#find() method can search for partial matches inside an input string, "a.c" will find a match in "1.0 abc.". The anchored "^a.c$" pattern will match an "abc" string, but won't find a match in "1.0 abc.".

此外,未锚定的正则表达式可能意味着处理正则表达式模式的代码不会检查匹配是否等于完整的输入字符串.例如.在 Java 中,Matcher#matches() 方法要求模式必须匹配完整的输入字符串并且 s.matches("ac") 将匹配一个 "abc" 字符串,但在 "1.0 abc." 中找不到匹配项.

Also, an unanchored regex may mean the code that handles the regex pattern does not check if the match is equal to full input string. E.g. in Java, Matcher#matches() method requires that the pattern must match the full input string and s.matches("a.c") will match an "abc" string, but won't find a match in "1.0 abc.".

锚定正则表达式意味着如果整个字符串匹配,模式将只匹配一个字符串.

Anchored regex means the pattern will only match a string if the whole string matches.

查看 字符串开头和字符串结尾锚点,了解有关正则表达式中锚点的更多信息.

See Start of String and End of String Anchors for more information about anchors in regex.

这篇关于锚定正则表达式和非锚定正则表达式有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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