LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-21-2009, 01:52 AM   #1
onesikgypo
Member
 
Registered: Jun 2008
Posts: 56

Rep: Reputation: 15
Extract everything before a regex match


Hi, say i had a string

"VALUE1 VALUE2 VALUE3 ID266PC32 VALUE5 Value6"

im having a bit of trouble on how i can extract value1 value2 value3 - before a regex match of ID[0-9][0-9][0-9]PC[0-9][0-9]

Doing:

echo `expr match "$testa" '.*\(ID[0-9][0-9][0-9]PC[0-9][0-9]*\)'`

will result in returning ID266PC32 - but im confused as to how i can extract everything before that - especially since there may be x values before that regex match.

Thanks.
 
Old 10-21-2009, 03:33 AM   #2
lutusp
Member
 
Registered: Sep 2009
Distribution: Fedora
Posts: 835

Rep: Reputation: 102Reputation: 102
Quote:
Originally Posted by onesikgypo View Post
Hi, say i had a string

"VALUE1 VALUE2 VALUE3 ID266PC32 VALUE5 Value6"

im having a bit of trouble on how i can extract value1 value2 value3 - before a regex match of ID[0-9][0-9][0-9]PC[0-9][0-9]

Doing:

echo `expr match "$testa" '.*\(ID[0-9][0-9][0-9]PC[0-9][0-9]*\)'`

will result in returning ID266PC32 - but im confused as to how i can extract everything before that - especially since there may be x values before that regex match.

Thanks.
Very simple:

Code:
$ x="VALUE1 VALUE2 VALUE3 ID266PC32 VALUE5 Value6"

$ echo ${x/ID[0-9][0-9][0-9]PC[0-9][0-9]*}
VALUE1 VALUE2 VALUE3
 
Old 10-21-2009, 03:45 AM   #3
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Code:
echo `expr match "$testa" '\(.*\)ID[0-9][0-9][0-9]PC[0-9][0-9]'`
 
Old 10-21-2009, 03:46 AM   #4
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
The output is the part betweem \( and \)
Code:
echo `expr match "$testa" '\(.*\)ID[0-9][0-9][0-9]PC[0-9][0-9]'`
 
Old 10-21-2009, 04:17 AM   #5
onesikgypo
Member
 
Registered: Jun 2008
Posts: 56

Original Poster
Rep: Reputation: 15
Great, thankyou very much
 
Old 10-21-2009, 04:49 AM   #6
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Code:
#!/bin/bash
a="VALUE1 LUE2 VALUE3 ID266PC32 VALUE1 VALUE2 VALUE3 ID266PC32 VALUE5 Value6 ID266PC32 blah1 blah2 blah3 ID266PC32 last1 last2 last3"
while true
do
  case "$a" in
    *ID266* ) 
        echo ${a%%ID266*}  
        a=${a#*ID266* }
        continue
        ;;
  esac
  break
done
output
Code:
# ./shell.sh
VALUE1 LUE2 VALUE3
VALUE1 VALUE2 VALUE3
VALUE5 Value6
blah1 blah2 blah3
or
Code:
a="VALUE1 LUE2 VALUE3 ID266PC32 VALUE1 VALUE2 VALUE3 ID266PC32 VALUE5 Value6 ID266PC32 blah1 blah2 blah3 ID266PC32 last1 last2 last3"
a="${a//ID266PC32/|}"
IFS="|"
set -- $a
for i in "${@}"
do
    echo $i
done

Last edited by ghostdog74; 10-21-2009 at 05:12 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
A single regex to match anything with ".aac" or ".mp3" at the end ? lumix Linux - General 9 05-09-2008 01:11 AM
Perl Regex multiline match issues adymcc Linux - General 2 03-31-2008 09:45 AM
how to look for the shortest match using regex, bascially the opposite of .* new_2_unix Linux - Newbie 8 01-08-2008 09:21 AM
regex with sed to process file, need help on regex dwynter Linux - Newbie 5 08-31-2007 05:10 AM
help me match this regex line (easy) JustinHoMi Programming 7 03-17-2002 01:43 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:27 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration