tags:

views:

83

answers:

4
+1  Q: 

regular expression

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.

+1  A: 

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.

rjh
this work great, thanks guys :) I really appreciate every help
Jeeenda
+1  A: 
echo '("./something\", [something.sh", ["./something\", [something.sh"], [/* 37 vars */])' |
sed 's/.*\(\.\/something\\", \[something.sh"\).*/\1/'
Jonathan Leffler
A: 

This doesn't directly amswer 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.

Dave
+1  A: 

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
J.F. Sebastian