Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
738 views
in Technique[技术] by (71.8m points)

perl regex escape characters

I have heard perl is a good language at doing regex but i am a bit confused at the characters that requires escaping

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

However, I am not quite sure how do i translate this to perl code

I tried,

//home/dev/((.*?)/[^/]+).*#

and

//home/dev/((.*?)/[^/]+).*#

and both failed

Don't you think the escaping makes the regex very unreadable? Am i using something wrong?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If you're using perl you don't have to use / as the regex delimiter if you preceed the delimiter with "m" for the matching operator, or "s" for the substitution operator (e.g. you can use # or ! or even any balanced parentheses/brackets: s[this][that]), and then you don't have to escape /. You can also use the quotemeta function or QE regex escape sequences that automatically escape any metacharacters.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...