Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > reg exp: helping hand needed

Reply
Thread Tools

reg exp: helping hand needed

 
 
Oliver Meister
Guest
Posts: n/a
 
      11-20-2006
Hello all

I might ask a quite trivial question but I don't get any further -
please excuse.

The following content is held in a string "$SingleMessage".
---------------------------------------------
1035 Zxxxxxxx
2317 XXXXXXXXXXXXXXXXXXXX
940 02
:20:XXXXXX202
:25:XXX25XXXXXXXXXXXX
:28:XXXXXXXXXXXXXXXXXXXX1
X XXXXXXXXXX XX XXXXXX X XXXXXXXXXX XX XXXXXX
X XXXXXXXXXX XX XXXXXX
:60F:XXXXXXXXXXXXXXXXXXXX1
:62F:XXXX222XXXXXXXXXXX
XXXX222XXXXXXXXXXX XXXXXXXXXX XX XXXXXX
:64:XXXXXXXXXXXXXXXXXXXX
---------------------------------------------

I'd like to receive the value, where ":<nn>:" is the field seperator
and "xxxx" the value, until the next field.
In other words: I'd like to return the values in between two
seperators, selected by a seperator.

I was trying with $fld_28 =~ s/(:28(.*?)/$1/ ; print $1;
This prints ":28:" only ...

I guess, that the s/ option isn't suitable (?).

May somebody lend me a helping hand?

Regards,
Oliver

 
Reply With Quote
 
 
 
 
Paul Lalli
Guest
Posts: n/a
 
      11-20-2006
Oliver Meister wrote:
> I might ask a quite trivial question but I don't get any further -
> please excuse.
>
> The following content is held in a string "$SingleMessage".
> ---------------------------------------------
> 1035 Zxxxxxxx
> 2317 XXXXXXXXXXXXXXXXXXXX
> 940 02
> :20:XXXXXX202
> :25:XXX25XXXXXXXXXXXX
> :28:XXXXXXXXXXXXXXXXXXXX1
> X XXXXXXXXXX XX XXXXXX X XXXXXXXXXX XX XXXXXX
> X XXXXXXXXXX XX XXXXXX
> :60F:XXXXXXXXXXXXXXXXXXXX1
> :62F:XXXX222XXXXXXXXXXX
> XXXX222XXXXXXXXXXX XXXXXXXXXX XX XXXXXX
> :64:XXXXXXXXXXXXXXXXXXXX
> ---------------------------------------------
>
> I'd like to receive the value, where ":<nn>:" is the field seperator


I think you mean that a "separator" is a colon, two digits, and another
colon. Okay, but what about ":60F:" and ":62F:" ? Are those
separators too? They don't match your description.

> and "xxxx" the value, until the next field.
> In other words: I'd like to return the values in between two
> seperators, selected by a seperator.
>
> I was trying with $fld_28 =~ s/(:28(.*?)/$1/ ; print $1;
> This prints ":28:" only ...


Of course it does. What were you expecting that to do? You searched
for ":28:" (saving it in $1), followed by the fewest number of
any-character (except the newline) that could be found. That of
course, is 0, so $2 is the empty string. Then you replaced everything
you matched - which, again, was only ":28:" - with whatever was in $1,
which was ":28:". So the string doesn't change at all, and $1 is
printed out after the s///. No idea what made you think that would do
anything else.

> I guess, that the s/ option isn't suitable (?).


I have no idea what that means. Are you talking about the s///
operator, or the /s option? The s/// operator is for modifying a
string. So no, you don't want to use that. You just want to pattern
match, ie, the m// operator. The /s option allows the . wildcard to
match newlines, so yes you do want that.

You want to match all the characters, including newlines, that exist
between ":28:" and the next insance of colon, two digits, colon. So do
that:

if ($fld_28 =~ /:28.*?):\d{2}:/){
print "Value: $1";
}

Paul Lalli

 
Reply With Quote
 
 
 
 
Paul Lalli
Guest
Posts: n/a
 
      11-20-2006
Oliver Meister wrote:
> I might ask a quite trivial question but I don't get any further -
> please excuse.
>
> The following content is held in a string "$SingleMessage".
> ---------------------------------------------
> 1035 Zxxxxxxx
> 2317 XXXXXXXXXXXXXXXXXXXX
> 940 02
> :20:XXXXXX202
> :25:XXX25XXXXXXXXXXXX
> :28:XXXXXXXXXXXXXXXXXXXX1
> X XXXXXXXXXX XX XXXXXX X XXXXXXXXXX XX XXXXXX
> X XXXXXXXXXX XX XXXXXX
> :60F:XXXXXXXXXXXXXXXXXXXX1
> :62F:XXXX222XXXXXXXXXXX
> XXXX222XXXXXXXXXXX XXXXXXXXXX XX XXXXXX
> :64:XXXXXXXXXXXXXXXXXXXX
> ---------------------------------------------
>
> I'd like to receive the value, where ":<nn>:" is the field seperator


I think you mean that a "separator" is a colon, two digits, and another
colon. Okay, but what about ":60F:" and ":62F:" ? Are those
separators too? They don't match your description.

> and "xxxx" the value, until the next field.
> In other words: I'd like to return the values in between two
> seperators, selected by a seperator.
>
> I was trying with $fld_28 =~ s/(:28(.*?)/$1/ ; print $1;
> This prints ":28:" only ...


Of course it does. What were you expecting that to do? You searched
for ":28:" (saving it in $1), followed by the fewest number of
any-character (except the newline) that could be found. That of
course, is 0, so $2 is the empty string. Then you replaced everything
you matched - which, again, was only ":28:" - with whatever was in $1,
which was ":28:". So the string doesn't change at all, and $1 is
printed out after the s///. No idea what made you think that would do
anything else.

> I guess, that the s/ option isn't suitable (?).


I have no idea what that means. Are you talking about the s///
operator, or the /s option? The s/// operator is for modifying a
string. So no, you don't want to use that. You just want to pattern
match, ie, the m// operator. The /s option allows the . wildcard to
match newlines, so yes you do want that.

You want to match all the characters, including newlines, that exist
between ":28:" and the next insance of colon, two digits, colon. So do
that:

if ($fld_28 =~ /:28.*?):\d{2}:/s){
print "Value: $1";
}

Paul Lalli

 
Reply With Quote
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      11-20-2006
Oliver Meister wrote:
> The following content is held in a string "$SingleMessage".
> ---------------------------------------------
> 1035 Zxxxxxxx
> 2317 XXXXXXXXXXXXXXXXXXXX
> 940 02
> :20:XXXXXX202
> :25:XXX25XXXXXXXXXXXX
> :28:XXXXXXXXXXXXXXXXXXXX1
> X XXXXXXXXXX XX XXXXXX X XXXXXXXXXX XX XXXXXX
> X XXXXXXXXXX XX XXXXXX
> :60F:XXXXXXXXXXXXXXXXXXXX1
> :62F:XXXX222XXXXXXXXXXX
> XXXX222XXXXXXXXXXX XXXXXXXXXX XX XXXXXX
> :64:XXXXXXXXXXXXXXXXXXXX
> ---------------------------------------------
>
> I'd like to receive the value, where ":<nn>:" is the field seperator
> and "xxxx" the value, until the next field.
> In other words: I'd like to return the values in between two
> seperators, selected by a seperator.


Storing the key/value pairs in a hash may or may not be what you want:

my %hash;
while ( $SingleMessage =~ /\w+).+?)(?=:\w+$)/gs ) {
$hash{$1} = $2;
}

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
 
Reply With Quote
 
Oliver Meister
Guest
Posts: n/a
 
      11-20-2006

Paul Lalli schrieb:

> Oliver Meister wrote:

....
>
> I think you mean that a "separator" is a colon, two digits, and another
> colon. Okay, but what about ":60F:" and ":62F:" ? Are those
> separators too? They don't match your description.
>


True! And yes, they are seperators too.

....

> >
> > I was trying with $fld_28 =~ s/(:28(.*?)/$1/ ; print $1;
> > This prints ":28:" only ...

>

....

> you matched - which, again, was only ":28:" - with whatever was in $1,
> which was ":28:". So the string doesn't change at all, and $1 is
> printed out after the s///. No idea what made you think that would do
> anything else.


A Lack of wisdom!

....

> You want to match all the characters, including newlines, that exist
> between ":28:" and the next insance of colon, two digits, colon. So do
> that:
>
> if ($fld_28 =~ /:28.*?):\d{2}:/s){
> print "Value: $1";
> }
>
> Paul Lalli


Thank you. Looking at it now, I SHOULD have figured out on my own. I
was too sure that I need an option like /s or /m....

Well, I'll figure out now (on my own), how I split on a single minus
sign without catching on anything else what includes a minus sign.

Thanks,
Oliver

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Helping Hand Case Mod??? bigal Case Modding 16 04-17-2006 08:29 AM
Giving Password Manager a helping hand ... Ken Firefox 3 03-19-2006 02:55 AM
a helping hand please robin Javascript 4 06-19-2004 01:46 AM



Advertisments