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!

Regular expression help

Status
Not open for further replies.

uuperl

Programmer
Feb 20, 2006
33
US
Hi,

I have the following pattern:

echo "Full Name [TEXT]" | sed 's/.* //'
output will be [TEXT]

echo "[TEXT] Full Name" | sed 's/ .*//'
output will be [TEXT]

How to parse if [TEXT] in the middle, such as
"FULL [TEXT] Name" or "FULL Name [TEXT] 123-902-1254"

How to print out only the [TEXT] part?

Thanks!
 
A couple of questions:[ol][li]Is [TEXT] always the same, or do you want to capture everything inside [ and ] ?[/li][li]What have you code so far? Post it, and we'll have a look at it.[/li][/ol]

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
Hello Steve,

I want capture everything inside [ ] and the text inside it.

basically i just use grep to capture the pattern in a file.

Thanks!
 
try this...
Perl:
use strict;
use warnings;

while (<>) {
   print "$1\n" if /\[([^\]]+)/;
}

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
Thanks Steve, i always want to understand something before I want to use it. can you explain to me the following line:

print "$1\n" if /\[([^\]]+)/;

Thanks!
 
OK
Perl:
print : the print function
$1    : the string captured by the regex in the parentheses
\n    : newline
if    : a regex returns true if it matches 
/     : opening 'quote' for the regex
\[    : escaped [ to make it a literal in the regex
(     : start of capturing parentheses
[^\]] : character class meaning 'anything except ]'
+     : occurring one or more times
)     : end of capturing parentheses
/     : closing 'quote' for the regex

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top