Extracting specific info from a string

Y

Yannick Turgeon

Hello all,

I just started to use perl and I'm having a string manipulation question:

Say we've got a file (which I load into a variable) with these data in it:
---- Beginning of file after this line --------
My list
A: cat
B: dog
C: horse
A: cow
C: bird
---- End of file before this line --------

or the equivalent:
$myFileString = "My list\nA: cat\nB: dog\nC: horse\nA: cow\nC: bird";

What I'd like to do is to extract the animal associated with the FIRST
occurence of a label. Here with the label "A:" I'd like to remove
everything before and after "cat" (not "cow").

How could I do that? Using regexp? I tryed the following to remove what
was before "cat":

$myFileString =~ s/ ^ .* A:\ //sx;

but this remove everything before "cow" instead of "cat".

Anybody can help?

One more thing. I use the "s" option but I don't undestand it quite well.
On the net, I've found the following:
"This option treats the string as a single line."
in which situation this could be a problem or something we don't want?

TIA

Yannick
 
J

James E Keenan

Yannick Turgeon said:
Hello all,

I just started to use perl and I'm having a string manipulation question:

Say we've got a file (which I load into a variable) with these data in it:
---- Beginning of file after this line --------
My list
A: cat
B: dog
C: horse
A: cow
C: bird
---- End of file before this line --------

or the equivalent:
$myFileString = "My list\nA: cat\nB: dog\nC: horse\nA: cow\nC: bird";

What I'd like to do is to extract the animal associated with the FIRST
occurence of a label. Here with the label "A:" I'd like to remove
everything before and after "cat" (not "cow").

How could I do that? Using regexp? I tryed the following to remove what
was before "cat":

$myFileString =~ s/ ^ .* A:\ //sx;

but this remove everything before "cow" instead of "cat".

Anybody can help?

Read up on non-greedy matching:
$myFileString =~ m|^.*?A:\s(.*?)\n|sx;
One more thing. I use the "s" option but I don't undestand it quite well.
On the net, I've found the following:
"This option treats the string as a single line."
in which situation this could be a problem or something we don't want?
Your situation is a case of a multi-line string, i.e., a string with
embedded newlines. Hence, it is an appropriate case for use of the /s
modifier. It is probably more common, however, to parse a file
line-by-line, attempting pattern matches on each line in succession. This
latter would *not* be a likely place to use the /s modifier.

jimk
 
T

Tad McClellan

^^^^^^^^^^^

I suggest using some other form of munging.

That particular one is likely to be widely killfiled...

One more thing. I use the "s" option but I don't undestand it quite well.


The "s" option makes dot match a newline.

in which situation this could be a problem or something we don't want?


When you want the dots in your pattern to match newlines. :)
 
Y

Yannick Turgeon

Ted

My limited english does not allow me to know what you mean by "to be
widely killfiled"... and to chose a different one that is not if I'd like!

Thanks for the precision on the "s" option. It's clear now.

Yannick
 
T

Tad McClellan

[ time-reversed text reversed back to actual time order ]


[ attribution (to me) missing here somewhere ]
My limited english does not allow me to know what you mean by "to be
widely killfiled"... and to chose a different one that is not if I'd like!


When you assume an alias, you get whatever reputation goes with it.

Addresses at nowhere.com have posted here before (try looking some
up at groups.google.com) and I, at least, have that whole domain
scored down a bunch.


( killfiled is when someone instructs their newsreader to delete
_automatically_ some articles so that the human user never even
sees them.

a score file assigns "scores" based on rules, then deletes
articles over some threshhold score set by the user.

in other words: many experienced people will not see articles
that have "nowhere.com" in their address, so you'll get less
answers to your questions if you use it.
)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top