RegEx challenge - jrs

Discussion in 'Perl Misc' started by John R, Nov 16, 2004.

  1. John R

    John R Guest

    I'm puzzled on how to make the following regex work:

    String(1):
    dsr:"Some text"; height:67; width:10; client:"seven"
    String(2):
    dsr:"Some;text"; height:67; width:10; client:"seven"

    RegEx applied:
    (\w+):?([^;]+)?;\s?

    String(1) parsed great, resulting in two arrays:
    dsr "Some text"
    height 67
    width 10
    client "seven"

    However, string(2) will not parse correctly because it has an extra
    ";" between the quotes:

    Can regex ignore ; if they are between quotes?
     
    John R, Nov 16, 2004
    #1
    1. Advertising

  2. (John R) wrote in news:100a40e5.0411151739.519e8b59
    @posting.google.com:

    > I'm puzzled on how to make the following regex work:
    >
    > String(1):
    > dsr:"Some text"; height:67; width:10; client:"seven"
    > String(2):
    > dsr:"Some;text"; height:67; width:10; client:"seven"


    Yeah whatever ... An almost identical question was posted and answered
    withing the last couple of days. Doesn't anyone lurk and read the FAQ any
    more?

    perldoc -q inside

    By the way:

    > RegEx applied:
    > (\w+):?([^;]+)?;\s?
    >
    > String(1) parsed great, resulting in two arrays:


    What do you mean "resulting in two arrays"?

    use strict;
    use warnings;

    my @data;

    while(<DATA>) {
    if( /^dsr:(".+"); height:(\d+); width:(\d+); client:(".+")\s*$/ ) {
    push @data, {
    dsr => $1,
    height => $2,
    width => $3,
    client => $4,
    };
    }
    }

    use Data::Dumper;
    print Dumper \@data;


    __DATA__
    dsr:"Some text"; height:67; width:10; client:"seven"
    dsr:"Some;text"; height:67; width:10; client:"seven"



    --
    A. Sinan Unur
    d
    (remove '.invalid' and reverse each component for email address)
     
    A. Sinan Unur, Nov 16, 2004
    #2
    1. Advertising

  3. John R

    John R Guest

    Re: RegEx challenge - doesn't work

    "A. Sinan Unur" <> wrote in message news:<Xns95A2D6215D1Casu1cornelledu@132.236.56.8>...
    > while(<DATA>) {
    > if( /^dsr:(".+"); height:(\d+); width:(\d+); client:(".+")\s*$/ ) {
    > push @data, {
    > dsr => $1,
    > height => $2,
    > width => $3,
    > client => $4,
    > };
    > }
    > }


    Thanks for the effort, but this will not work either. Your regex is
    pattern matching the specific words "dsr" "height" etc... Maybe I
    should have mentioned this earlier, the name/value pairs cannot be
    predicted. The expected format is:
    name:value; name:value; name:"value blah"; name:"valu;e"

    > Yeah whatever ... An almost identical question was posted and answered
    > withing the last couple of days. Doesn't anyone lurk and read the FAQ any
    > more?


    Give me some more credit here. I read the FAQ. This is a unique
    situation.

    > What do you mean "resulting in two arrays"?


    RegEx option /m like /(\w+):?([^;]+)?;\s?/m for multiple matches
    you'll get an array foreach grouping () eachtime the pattern is
    repeatedly matched.

    Does anyone understand my question?

    <snip>
     
    John R, Nov 16, 2004
    #3
  4. Re: RegEx challenge - doesn't work

    (John R) wrote in
    news::

    > "A. Sinan Unur" <> wrote in message
    > news:<Xns95A2D6215D1Casu1cornelledu@132.236.56.8>...
    >> while(<DATA>) {
    >> if( /^dsr:(".+"); height:(\d+); width:(\d+); client:(".+")\s*$/ )
    >> {
    >> push @data, {
    >> dsr => $1,
    >> height => $2,
    >> width => $3,
    >> client => $4,
    >> };
    >> }
    >> }

    >
    > Thanks for the effort, but this will not work either. Your regex is
    > pattern matching the specific words "dsr" "height" etc... Maybe I
    > should have mentioned this earlier, the name/value pairs cannot be
    > predicted. The expected format is:
    > name:value; name:value; name:"value blah"; name:"valu;e"


    The quality of the help you get is in direct proportion to the amount of
    effort you put into formulating your question. Your question contained an
    incomplete description of the problem.

    Please see http://www.catb.org/~esr/faqs/smart-questions.html

    >> Yeah whatever ... An almost identical question was posted and
    >> answered withing the last couple of days. Doesn't anyone lurk and
    >> read the FAQ any more?

    >
    > Give me some more credit here. I read the FAQ. This is a unique
    > situation.


    Your situation is not unique. You have not read the FAQ.
    >
    >> What do you mean "resulting in two arrays"?

    >
    > RegEx option /m like /(\w+):?([^;]+)?;\s?/m for multiple matches
    > you'll get an array foreach grouping () eachtime the pattern is
    > repeatedly matched.


    Each time you capture matches, the match in list context returns a list
    of matches. There are no arrays involved. Again, it is time to hit the
    FAQ list.

    > Does anyone understand my question?


    Oh yes. And your question has been answered completely by the FAQ
    reference I posted.

    Sinan
     
    A. Sinan Unur, Nov 16, 2004
    #4
  5. Re: RegEx challenge - doesn't work

    John R <> wrote:


    > RegEx option /m like /(\w+):?([^;]+)?;\s?/m for multiple matches
    > you'll get an array foreach grouping () eachtime the pattern is
    > repeatedly matched.



    m//m has absolutely nothing to do with multiple matches.

    m//m changes the meaning for only the ^ and $ anchor,
    it *does nothing* for your pattern above because your
    pattern does not contain ^ or $ anchors.

    m// in *list context* returns a list of all the matching memories.


    > Does anyone understand my question?



    It sounds just like this question:

    How can I split a [character] delimited string except when inside
    [character]? (Comma-separated files)

    Do you understand the answer given along with it?


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Nov 16, 2004
    #5
  6. John R

    gnari Guest

    Re: RegEx challenge - doesn't work

    "John R" <> wrote in message
    news:...
    > "A. Sinan Unur" <> wrote in message

    news:<Xns95A2D6215D1Casu1cornelledu@132.236.56.8>...
    > >[snip nice solution based on original description]

    >
    > Thanks for the effort, but this will not work either. Your regex is
    > pattern matching the specific words "dsr" "height" etc... Maybe I
    > should have mentioned this earlier, the name/value pairs cannot be
    > predicted. The expected format is:
    > name:value; name:value; name:"value blah"; name:"valu;e"
    >
    > [snip]
    >
    > RegEx option /m like /(\w+):?([^;]+)?;\s?/m for multiple matches


    actually, your regex seems to imply more that your 'expected format' above.
    is the colon optional ?
    is the value itself optional ?
    doesn't the last name-value pair require a terminating semicolon ?

    >
    > Does anyone understand my question?


    don't be so arrogant when you are asking for help.

    maybe you want something like: /(\w+):("[^"]+"|[^;]+);\s?/m
    but in any case read the faq entry you have been referred to.

    gnari
     
    gnari, Nov 16, 2004
    #6
  7. John R

    gnari Guest

    Re: RegEx challenge - doesn't work

    "gnari" <> wrote in message
    news:cndsk4$71k$...
    > "John R" <> wrote in message
    > news:...
    > > [stuff]

    > maybe you want something like: /(\w+):("[^"]+"|[^;]+);\s?/m


    aargh. the /m was mindlessly copied from John's code.

    serves me right to reply to posts that do not really need it.

    gnari
     
    gnari, Nov 16, 2004
    #7
  8. Re: RegEx challenge - still looking

    "John R." <> wrote in news:419ba692$1_1@127.0.0.1:

    > Tad McClellan wrote:
    >> It sounds just like this question:
    >>
    >> How can I split a [character] delimited string except when
    >> inside [character]? (Comma-separated files)
    >>
    >> Do you understand the answer given along with it?
    >>

    >
    > Yes, this is much closer to my question.


    This is the exact same FAQ entry I directed you to in my first response.

    ....

    > Again, regex is used an a lot of langs, so I'm just after the
    > expression itself.


    Do you have a Perl question or not?

    Feel free to study the source code of the modules mentioned in the FAQ
    answer to see how it is done.

    Sinan.
     
    A. Sinan Unur, Nov 17, 2004
    #8
    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. Roedy Green

    Regex challenge

    Roedy Green, Jun 4, 2008, in forum: Java
    Replies:
    15
    Views:
    588
    BTDTGTTS
    Jun 5, 2008
  2. Replies:
    3
    Views:
    773
    Reedick, Andrew
    Jul 1, 2008
  3. basi
    Replies:
    3
    Views:
    86
  4. Max Metral

    HTML regex challenge

    Max Metral, Jul 24, 2004, in forum: Perl Misc
    Replies:
    5
    Views:
    139
    Tore Aursand
    Jul 25, 2004
  5. J R Stockton

    JRS & MERLYN, for info

    J R Stockton, Oct 19, 2006, in forum: Javascript
    Replies:
    1
    Views:
    64
    Martin Honnen
    Oct 20, 2006
Loading...

Share This Page