开发者

regular expression for file paths

开发者 https://www.devze.com 2022-12-23 19:52 出处:网络
Hi I need a regular expression that\'ll give me something like this part ./something\\开发者_开发问答\", [something.sh

Hi I need a regular expression that'll give me something like this part

./something\开发者_开发问答", [something.sh

from something like this string

("./something\", [something.sh", ["./something\", [something.sh"], [/* 37 vars */])

is that possible? I'm having real trouble making this since there's that \" escape sequence and also that ',' character, so I cannot simply use match everything instead of these characters. I'm working on unix so it's also possible to use pipeline of few greps or something like that. Thanks for advice.


With Perl you can use Text::Balanced which has an extract_quotelike function to do what you need.

You can do it manually with:

 /"((?:\\.|.)*?)"/

Basically: starting from a quote, if you see a \, grab the next character (even if it's a quote), else continue until you see a quote.


echo '("./something\", [something.sh", ["./something\", [something.sh"], [/* 37 vars */])' |
sed 's/.*\(\.\/something\\", \[something.sh"\).*/\1/'


Your input looks like Python literal. You can evaluate it as is:

$ echo '("./something\", [something.sh", ["./something\", [something.sh"], )' |\
> python -c'print input()[0]'

Output:

./something", [something.sh

Or for multiline case:

$ echo '("./something\", [something.sh", ["./something\", [something.sh"], )' |\
> python -c'import ast, fileinput
> for line in fileinput.input():
>      print ast.literal_eval(line)[0]'

Output:

./something", [something.sh


This doesn't directly answer your question, but my suggestion is to get your regex working quickly first, then write the code in whatever language you use.

This website is one that I always go to first to work on my regexes: www.regexr.com

It's really nice because it'll show you groupings, too.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号