regular expressions

Discussion in 'Perl Misc' started by mud_saisem, Aug 9, 2009.

  1. mud_saisem

    mud_saisem Guest

    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";
    mud_saisem, Aug 9, 2009
    #1
    1. Advertising

  2. 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.
    Alexander Bartolich, Aug 9, 2009
    #2
    1. Advertising

  3. mud_saisem

    mud_saisem Guest

    On Aug 10, 8:42 am, Alexander Bartolich <>
    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
    mud_saisem, Aug 9, 2009
    #3
  4. mud_saisem

    Guest

    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
    , Aug 10, 2009
    #4
  5. mud_saisem

    mud_saisem Guest

    On Aug 11, 5:19 am, wrote:
    > 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


    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.
    mud_saisem, Aug 11, 2009
    #5
  6. mud_saisem

    Nathan Keel Guest

    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.
    Nathan Keel, Aug 11, 2009
    #6
  7. mud_saisem

    Guest

    On Mon, 10 Aug 2009 16:43:21 -0700 (PDT), mud_saisem <> wrote:

    >On Aug 11, 5:19 am, wrote:
    >> 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

    <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
    , Aug 11, 2009
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jay Douglas

    Custom Regular Expressions in ASP.net

    Jay Douglas, Nov 2, 2003, in forum: ASP .Net
    Replies:
    3
    Views:
    605
    mikeb
    Nov 3, 2003
  2. mark

    Regular expressions

    mark, Jun 30, 2003, in forum: Perl
    Replies:
    4
    Views:
    1,719
  3. Dustin D.
    Replies:
    1
    Views:
    11,198
  4. Jay Douglas
    Replies:
    0
    Views:
    600
    Jay Douglas
    Aug 15, 2003
  5. Noman Shapiro
    Replies:
    0
    Views:
    232
    Noman Shapiro
    Jul 17, 2013
Loading...

Share This Page