Posted by Jason Nugent on September 15, 1998 at 12:12:35:
In Reply to: Perl string manipulation posted by Bob on September 15, 1998 at 10:03:13:
: I have a perl variable that contains this text (it is output from a database query) :
: |password | +----------+ |rangers |
: (Password is the column name, rangers the data).
: Can anyone suggest how to extract the 'rangers' part of the string?
Regular expressions really boil down to knowing your data. If you are sure that you will always have this format, you might want to try:
$data =~ s/| (\w+)|$/$1/; # neato regex
let's go through it:
$data is your original string. you want to match the "rangers" part of the string at the end, which is indicated by the last section of pipe characters followed by and "end of string".
| (\w+)| will match anything like |this is some text|. We anchor it to the end using a $ character so it won't match your password field. It's enclosed in ()'s because we want to store it for the substitution part of the regex later on. after a sucessful match, $1 contains the matched text. By using it by itself in the substitution, everything else is stripped out.