Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Replacing a pattern

Status
Not open for further replies.

dlabdo

Programmer
Sep 6, 2005
10
0
0
US
Im trying to move around parts of a string a get rid of some of it too. Below is an example...

$str = "a b c d e f g VALUES(h i j k l m n) Bound Values: o|p|q|r|s|t|u|";

What i need to do is move the o|p|q|r|s|t|u| into the parenthsis after "VALUES" and then erase the stuff after it. I know how to erase the stuff (substr = "") but im having trouble moving the pattern. Is there a way i can assign a whole pattern's results to a variable?

ex: $str = "fjdsfds(fdjfksdjflds)fjdsklfjds";
$t = m/(*)/; ???? #basically get everything in between the parenthesis???
 
you can use the special variables $1, $2, $3, etc etc etc to store pattern matches in regexp's. Here is one possibility for the string you posted using a regexp:

Code:
my $str = "a b c d e f g VALUES(h i j k l m n) Bound Values: o|p|q|r|s|t|u|";
$str =~ s/^([a-z]+ VALUES)\(([a-z ]+)\)( Bound Values: )(.+)$/$1 ($2 $4)/;
print $str;

$1 holds the pattern match from the first set of parenthesis, $2 the next, $3 the next, and so on. You can nest parenthesis too for more complex pattern matching.


I'm sure this can also be done using index() and substr()
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top