LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Quick regex problem, can't find solution (https://www.linuxquestions.org/questions/programming-9/quick-regex-problem-cant-find-solution-327058/)

R00ts 05-25-2005 02:21 PM

Quick regex problem, can't find solution
 
I've been playing around with a few regular expressions for half an hour now and I'm stumped on what to do. Here's the string I want to apply the regex to:

./crafty_base.gnu3 < crafty.in > crafty.out 2> crafty.err

What I want to do is extract everything between the <, > and 2>. I've managed to grab everything but crafty.out now. Here's my bash script:

Code:

# exec_target contains the initial string I want to perform the regex on
exec_in=$echo $(echo exec_target | sed 's/.*<//g') | sed 's/>.*//g')
exec_out=$(echo exec_target | sed 's/.*?^2>//g')
exec_err=$(echo exec_target | sed 's/.*2>//g')
echo "TARGET: $exec_target"
echo "INPUT:    $exec_input"
echo "OUTPUT: $exec_output"
echo "ERROR: $exec_err"

And the output of this script:

Code:

TARGET: ./crafty_base.gnu3
INPUT: crafty.in
OUTPUT: ./crafty_base.gnu3 < crafty.in > crafty.out 2> crafty.err
ERROR: crafty.err


What I want to tell sed is "eat up everything that occurs before the first >", but no matter what I've tried it always eats up everything before the last ">". I tried to fix this by saying that the 2 character can not preceed the > but that doesn't seem to be working. And setting .* to be ungreedy causes nothing to be eaten at all. :( I've tried googling, reading the sed manpage, and I just can't find a solution. Does anyone know how I can do this?


Oh and FYI: I can't do something like use the < for solving this problem, because I'm using these regexes for multiple strings, all of which <, > and 2> are completely optional.

towlie 05-25-2005 02:35 PM

Don't really know sed, but isn't this how you eliminate
a particular char (so it can't precede)?

Code:

[~2]

So something like:
Code:

s/.?*[~2]>//g


EDIT:
I'm used to perl compatible re's so if that doesn't work
with sed, you could maybe just switch it to a perl line
in your script instead of sed.

R00ts 05-25-2005 02:51 PM

Yes, sed is perl compatible. I've never seen ~ used as the negation operator though, I always thought it was ^


Well good news: that helped me figure out what was wrong. I needed [ ] around ^2. I think ^ is also the match beginning of line operator so maybe the interpreters needs it to be in square brackets for it to be interpreted as negation. This is the fix for output:

Code:

exec_out=$(echo $(echo $exec_target | sed 's/.*[^2]>//g') | sed 's/2>.*//g')
:D

towlie 05-25-2005 02:55 PM

What the, :scratch: hmmm, yeah you're so right. The ^ is
the negation op.

What was I thinking with ~?

Been a while with me for regexes :D


All times are GMT -5. The time now is 09:03 AM.