regular expressions

M

mud_saisem

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";
 
A

Alexander Bartolich

mud_saisem said:
[...]
$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
 
M

mud_saisem

mud_saisem said:
[...]
$ 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
 
S

sln

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
 
M

mud_saisem

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.
 
N

Nathan Keel

mud_saisem said:
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.
 
S

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
 

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,769
Messages
2,569,582
Members
45,058
Latest member
QQXCharlot

Latest Threads

Top