perl 正则表达式转义字符 [英] perl regex escape characters
问题描述
我听说 perl 是一种很好的正则表达式语言,但我对需要转义的字符有点困惑
I have heard perl is a good language at doing regex but i am a bit confused at the characters that requires escaping
我在 http://regexlib.com/RETester.aspx 上测试了代码并得到了结果我要
I tested the code on http://regexlib.com/RETester.aspx and got the result I want
//home/dev/abc/code/hello/world.cpp#1
//home/dev/((.*?)/[^/]+).*#
Match $1 $2
//home/dev/abc/code/hello/world.cpp# abc/code abc
但是,我不太确定如何将其转换为 perl 代码
However, I am not quite sure how do i translate this to perl code
我试过了,
\/\/home\/dev\/\(\(\.\*\?\)\/\[\^\/\]\+\)\.\*\#
和
\/\/home\/dev\/((.*?)\/[^\/]+).*\#
而且都失败了
您不认为转义会使正则表达式非常不可读吗?我用错了吗?
Don't you think the escaping makes the regex very unreadable? Am i using something wrong?
推荐答案
你可以选择使用 !
而不是 /
来包围你的正则表达式,这样你就不会必须转义 /
.
You can choose use !
instead of /
to surround your regular expression so that you don't have to escape the /
.
m!//home/dev/((.*?)/[^/]+).*#!
应该可以.实际操作如下:http://ideone.com/TDrBG
should work. Here's it in action: http://ideone.com/TDrBG
这篇关于perl 正则表达式转义字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!