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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need help with RegExp problem

Status
Not open for further replies.

DanKay1

Programmer
Jun 9, 2004
54
US
I need to pick up the company name from each line.
I can't write a regex that will pick up the string
"MUNICH AMERICAN (CNA)" or "MUNICH AMERICAN" from each line.
Basically, it has to pick up a string from the beginning of the line
to "Treaty" part and it should ignore Totals since it also has treaty string in it.

MUNICH AMERICAN (CNA) Treaty: 022
MUNICH AMERICAN Treaty: 034
Totals: Treaty: 099




Thank you.
 
Code:
$bla = "MUNICH AMERICAN (CNA)        Treaty: 022";
$bla =~ s/\s{2,}Treaty.*//;

now you have 

$bla = "MUNICH AMERICAN (CNA)"

as for the last line(if you dont want to pick it up) just dont read the last line of the file in your loop

 
I have hundreds of lines, I have to skip them all the time its not just at the end of the file.

is it possible to translate it into perl:
Pick up the lines where String starts with M and its between Treaty string.
 
How about something like:

Code:
while (<>) {
    m/^(.*)\b\s*Treaty/;
    print "$1\n";
}
 
rharsh this regex does not take the spaces out of the $_
the variable will look like his

$bla = "MUNICH AMERICAN (CNA) ";


DanKay1
if you have that many lines then try this

Code:
$bla =~ s/(Totals:)?\s{2,}Treaty.*//;
 
Should have tested it first. Try this:

Code:
m/^(.*?)\s*Treaty/;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top