首先说明:
1、哪边是前,哪边是后?
不像我们阅读一本书,排在前面的是前,排在后面的是后。
对于查找的游标(Cursor)来说,没有查找的是前,查找过的是后。
look-ahead :向未查找的字符串中查找。
look-behind :向已经查找过的字符串中查找。
2、loodahead 、lookbehind 出现之动机
为什么这么写呢?这牵涉到 Cursor 的移动问题。
这种操作并不移动 Cursor,不影响下一次查找。
一、Lookahead
向前匹配:(?=regEx)
> (regexp-match-positions #rx "grey(?=hound)"
"i left my grey socks at the greyhound")
'((28 . 32))
grey(?=hound): 该正则匹配 "grey",但是前面必须跟 "hound"。
向前不匹配:(?!regEx)
> (regexp-match-positions #rx"grey(?!hound)"
"the gray greyhound ate the grey socks")
'((27 . 31))
grey(?!hound): 该正则匹配 "grey",但是前面不能是 "hound"。
二、LookBehind
向后匹配:(?<=regEx)
> (regexp-match-positions #rx"(?<=grey)hound"
"the hound in the picture is not a greyhound")
'((38 . 43))
(?<=grey)hound: 该正则匹配 "hound",但后面必须是"grey"
向后不匹配:(?<!regEx)
> (regexp-match-positions #rx"(?<!grey)hound"
"the greyhound in the picture is not a hound")
'((38 . 43))
(?<!grey)hound: 该正则匹配 "hound",但后面不能是"grey"
注意:
JavaScript 不支持 lookbehind:(?<= ) or (?<! ),但是支持 lookahead: (?= ) or (?! ).
引用:
https://docs.racket-lang.org/guide/Looking_Ahead_and_Behind.html
-
转载请注明
原文出处:http://lixh1986.iteye.com/blog/2324010
-
分享到:
相关推荐
linux下的C语言正则表达式头文件和源文件: regex.h regex.cpp
64位的regex.dll 在mingw环境下编译成的,大家可以使用
易语言源码RegEx.rar 易语言源码RegEx.rar 易语言源码RegEx.rar 易语言源码RegEx.rar 易语言源码RegEx.rar 易语言源码RegEx.rar
Regular expression 具有可以表达出难以描述、複杂、但是却有特殊规则的字串的功能,所以许多的 UNIX 工具程式都有支援 Regular expression 的功能。例如 ex 、 vi 、 sed 、 awk 、 grep ...这里有文件Regex.h,Regex.c
GNU regex windows编译 regex2dll 替换成源码。可直接添加进工程使用
Regex Testor Version 1.02 Copyright (c) 2013 Fiery Red - flameleo 我们在使用正则表达式( regex: regular expression )的过程中,经常发现正则表达式的语法很令人头疼,即使对经常使用它的人来说也是如此。...
正则表达式regex.lib
linux下posix标准的正则表达式库,支持regcomp, regexec等,可以放到vs2010或vs2012下进行编译。
regex 正则软件regex 正则软件regex 正则软件regex 正则软件regex 正则软件 让你很好的上手测试正则表达式
免费工具 SDL Regex Fuzzer 用以帮助程序员测试他们的正则表达式是否存在 DoS 漏洞。 正则表达式模式中包含了某些执行起来极其耗时(指数时间)条款,(例如:分组条款中包含重复自身的重复)。这可能导致被攻击者...
C++Regex正则表达式
This resource is description how to use STL library of regex in cpp.
正则表达式头文件
Simple application for test string regex
#include <boost/regex.hpp> #include #include using namespace std; using namespace boost; regex e("^\\w+\\d+"); string text1("abc123"); string text2("123abc"); int main(int argc, char* argv[]) { ...
regex最简单使用的工具
一个.Net下的测试正则表达式 工具Regex Tester,打开压缩包,直接运行RegexTester.exe
Regex
正则表达式是一个很强的工具,可以在你的软件中增强查找、替换、匹配等功能。附件中封装了Henry Spencer的regex library源码,有说明和范例,纯C可移值,简单易用。
RegexTester.exe 正则表达测试。。。