Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > regular expressions

Reply
Thread Tools

regular expressions

 
 
mud_saisem
Guest
Posts: n/a
 
      08-09-2009
Hi There,

Could anybody please let me know how I would be able to extract the
words that have a = in the middle and print the whole word out. Also I
will never know how many : and in the word either.

eg: "This is a example item, order=TEST:ITEM:123 on shelf
shelf=1:4:23:f"

output: order=TEST:ITEM:123
shelf=1:4:23:f

This is what I have done so far but only prints out the word until the
first :

eg: order=TEST



#!/usr/bin/perl

system(clear);

$string = "This is a example item, order=TEST:ITEM:123 on shelf
shelf=1:4:23:f";
$str = $string;

print "String: $string\n\n";
sub extract
{
$x = shift;
print "Checking: $x\n";
return $x;
}

$string =~ s/(\w+=\w+)/extract $1/ge;

print "\n";
 
Reply With Quote
 
 
 
 
Alexander Bartolich
Guest
Posts: n/a
 
      08-09-2009
mud_saisem wrote:
> [...]
> $string =~ s/(\w+=\w+)/extract $1/ge;


$string =~ s/(\w+=[\w:]+)/extract $1/ge;

Checking: order=TEST:ITEM:123
Checking: shelf=1:4:23:f

--
Brüder, in die Tonne die Freiheit,
Brüder, ein Stoppschild davor.
Egal was die Schwarzen Verlangen
Rufen wir: Ja! Brav im Chor.
 
Reply With Quote
 
 
 
 
mud_saisem
Guest
Posts: n/a
 
      08-09-2009
On Aug 10, 8:42*am, Alexander Bartolich <alexander.bartol...@gmx.at>
wrote:
> mud_saisem wrote:
> > [...]
> > $ String = ~ s / (\ w + = \ w +) / $ 1/ge extract;

>
> $ string = ~ s / (\ w + = [\ w :]+)/ $ 1/ge extract;
>
> Checking: order = TEST: ITEM: 123
> Checking: shelf = 1:4:23: f
>
> --
> Brothers, in a ton of freedom,
> Brothers, a stop sign in front.
> Regardless of what the blacks request
> Call us: Yes! Brave in the choir.


Great work !!

Thanks
 
Reply With Quote
 
sln@netherlands.com
Guest
Posts: n/a
 
      08-10-2009
On Sun, 9 Aug 2009 15:25:24 -0700 (PDT), mud_saisem <> wrote:

>Hi There,
>
>Could anybody please let me know how I would be able to extract the
>words that have a = in the middle and print the whole word out. Also I
>will never know how many : and in the word either.
>
>eg: "This is a example item, order=TEST:ITEM:123 on shelf
>shelf=1:4:23:f"
>
>output: order=TEST:ITEM:123
> shelf=1:4:23:f
>
>This is what I have done so far but only prints out the word until the
>first :
>
>eg: order=TEST
>
>
>
>#!/usr/bin/perl
>
>system(clear);
>
>$string = "This is a example item, order=TEST:ITEM:123 on shelf
>shelf=1:4:23:f";
>$str = $string;
>
>print "String: $string\n\n";
>sub extract
>{
> $x = shift;
> print "Checking: $x\n";
> return $x;
>}
>
>$string =~ s/(\w+=\w+)/extract $1/ge;
>
>print "\n";


Just a comment on your example string.
You have a rambling/random, englishly, seemingly un-delimited
string. Then all of a sudden, there is this perfect, parsable
slug of data that doesen't fit:

order=TEST:ITEM:123

Instantly the data slug is on a \s boundry and triggered by '='.
This is not an example for anything. It could just as well have been:

order this = 'TEST:ITEM' : 123

In reality, constructing regular expressions from unknown/unreliable,
computer generated data falling in the range of repeatablility, including
variances, are an entirely different thing.

Learning word boundries and classes is one thing. Its obvious you know what
they are otherwise you would not have constructed such a blatant example that
applies to absolutely nothing else.

Constructing a quiz for the pupils?
Seriously, you should get more realistic examples. Plucking
' order=TEST:ITEM:123 ' from random wordy text is as basic as it comes.
Given your doing eval with func call in global context an all, have to wonder
about your motivations.

My opinion on this group (or any group) is that using real world examples
reap the most reward.

-sln
 
Reply With Quote
 
mud_saisem
Guest
Posts: n/a
 
      08-10-2009
On Aug 11, 5:19*am, s...@netherlands.com wrote:
> On Sun, 9 Aug 2009 15:25:24 -0700 (PDT), mud_saisem <mud_sai...@hotmail.com> wrote:
> >Hi There,

>
> >Could anybody please let me know how I would be able to extract the
> >words that have a = in the middle and print the whole word out. Also I
> >will never know how many : and in the word either.

>
> >eg: "This is a example item, order=TEST:ITEM:123 on shelf
> >shelf=1:4:23:f"

>
> >output: order=TEST:ITEM:123
> > * * * * * shelf=1:4:23:f

>
> >This is what I have done so far but only prints out the word until the
> >first :

>
> >eg: order=TEST

>
> >#!/usr/bin/perl

>
> >system(clear);

>
> >$string = "This is a example item, order=TEST:ITEM:123 on shelf
> >shelf=1:4:23:f";
> >$str = $string;

>
> >print "String: $string\n\n";
> >sub extract
> >{
> > * * * *$x = shift;
> > * * * *print "Checking: $x\n";
> > * * * *return $x;
> >}

>
> >$string =~ s/(\w+=\w+)/extract $1/ge;

>
> >print "\n";

>
> Just a comment on your example string.
> You have a rambling/random, englishly, seemingly un-delimited
> string. Then all of a sudden, there is this perfect, parsable
> slug of data that doesen't fit:
>
> * *order=TEST:ITEM:123
>
> Instantly the data slug is on a \s boundry and triggered by '='.
> This is not an example for anything. It could just as well have been:
>
> * *order this = 'TEST:ITEM' : 123
>
> In reality, constructing regular expressions from unknown/unreliable,
> computer generated data falling in the range of repeatablility, including
> variances, are an entirely different thing.
>
> Learning word boundries and classes is one thing. Its obvious you know what
> they are otherwise you would not have constructed such a blatant example that
> applies to absolutely nothing else.
>
> Constructing a quiz for the pupils?
> Seriously, you should get more realistic examples. Plucking
> *' order=TEST:ITEM:123 ' from random wordy text is as basic as it comes.
> Given your doing eval with func call in global context an all, have to wonder
> about your motivations.
>
> My opinion on this group (or any group) is that using real world examples
> reap the most reward.
>
> -sln


Sln,

Thanks for your comments.

The examples that I have given are based on problems that I can not
figure out and need help with.

If I put in real world example as you call it, It would still make no
sense to you or any other person reading this discussion. I ask for
help on what I need to resolve my issue and never intended this to be
a learning curve for pupils.

Personally I don't see what the issue is as others have managed to
give very good and efficient solutions to the problem that I face.

But I your point is taken !

I am still very new at perl, and in the future I will put in better
examples WHERE POSSIBLE.
 
Reply With Quote
 
Nathan Keel
Guest
Posts: n/a
 
      08-11-2009
mud_saisem wrote:

>
> Personally I don't see what the issue is as others have managed to
> give very good and efficient solutions to the problem that I face.


Ignore him, this is common. He rarely provides real help and often
gives broken code when he does try (which I can sort of appreciate),
but his motivation here is usually to either try and show off or berate
someone else. A lot of people have filtered his posts. Don't let it
get to you.
 
Reply With Quote
 
sln@netherlands.com
Guest
Posts: n/a
 
      08-11-2009
On Mon, 10 Aug 2009 16:43:21 -0700 (PDT), mud_saisem <> wrote:

>On Aug 11, 5:19*am, s...@netherlands.com wrote:
>> On Sun, 9 Aug 2009 15:25:24 -0700 (PDT), mud_saisem <mud_sai...@hotmail.com> wrote:
>> >Hi There,

>>
>> >Could anybody please let me know how I would be able to extract the
>> >words that have a = in the middle and print the whole word out. Also I
>> >will never know how many : and in the word either.

>>
>> >eg: "This is a example item, order=TEST:ITEM:123 on shelf
>> >shelf=1:4:23:f"

>>
>> >output: order=TEST:ITEM:123
>> > * * * * * shelf=1:4:23:f

<snip>
>>
>> My opinion on this group (or any group) is that using real world examples
>> reap the most reward.
>>
>> -sln

>
>Sln,
>
>Thanks for your comments.
>
>The examples that I have given are based on problems that I can not
>figure out and need help with.
>
>If I put in real world example as you call it, It would still make no
>sense to you or any other person reading this discussion. I ask for
>help on what I need to resolve my issue and never intended this to be
>a learning curve for pupils.
>
>Personally I don't see what the issue is as others have managed to
>give very good and efficient solutions to the problem that I face.
>
>But I your point is taken !
>
>I am still very new at perl, and in the future I will put in better
>examples WHERE POSSIBLE.


Sorry, I didn't mean it as criticism, just to be a little helpful.
It doesn't help you to put up example text that you want to
parse with a regular expression, in a waterred down, pristeen way.

This is your example:
"This is a example item, order=TEST:ITEM:123 on shelf
shelf=1:4:23:f"

But you state you really want this form "=::" to be plucked out using \w=[\w:]
Itself, its being used as a form and form delimeter. And thats ok.

In a real example though, its not that simple. The form itself must be
seperately delimited to avoid collision. That makes it more difficult.
And \w as a form delimeter rarely does what you think it does.
It leaves out many possible valid characters. You can class characters
that you would would like to INCLUDE as your only form delimeter, but it
is better to class NOT characters in the form, then surround the form itself
with delimeters.

Since the delimeters are simple whitespace, you could (as someone mentioned)
split on \s+, then analyse the form itself.

Or you could do, in a real sence, something like below.

-sln

use strict;
use warnings;

my $string = "begi-n==TEST:ITEM:b123
begin2=='TEST':ITEM:b-123 This is a example item, order=TEST:ITEM123:test= on shelf
'shelf'=1:4:*23::f";

while ($string =~ /(?:^|(?<=\s))([^\s:=]+)=+([^\s=]+)(?=\s|$)/g)
{
print "$1 = @{[split /:+/, $2]}\n";
}

__END__

begi-n = TEST ITEM b123
begin2 = 'TEST' ITEM b-123
'shelf' = 1 4 *23 f






 
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
Custom Regular Expressions in ASP.net Jay Douglas ASP .Net 3 11-03-2003 08:09 PM
Regular expressions mark Perl 4 10-28-2003 12:37 PM
perl regular expressions return last matched occurence? Dustin D. Perl 1 08-28-2003 01:51 AM
matching curly braces and regular expressions Dustin D. Perl 0 08-26-2003 11:18 PM
Add custom regular expressions to the validation list of available expressions Jay Douglas ASP .Net 0 08-15-2003 10:19 PM



Advertisments