regexp

Discussion in 'Perl Misc' started by Wojtek Michalik, Jun 3, 2004.

  1. Can anybody give me en example of working regex
    that will match sample text given below:
    ____________
    banana\n
    \tapple\n
    ____________
    \t=tab and \n=nweline
    I have been trying to match it by:
    perl -pi -e 's#(banana\n\t)(apple)#$1\.\/$2#g' with modifiers like m s ms
    nothing worked.
    I use perl version 5.8 on linux
     
    Wojtek Michalik, Jun 3, 2004
    #1
    1. Advertising

  2. In article <>,
    Wojtek Michalik <> wrote:
    :Can anybody give me en example of working regex
    :that will match sample text given below:
    :____________
    :banana\n
    :\tapple\n
    :____________
    :\t=tab and \n=nweline
    :I have been trying to match it by:
    :perl -pi -e 's#(banana\n\t)(apple)#$1\.\/$2#g' with modifiers like m s ms
    :nothing worked.

    You haven't really defined what your pattern is.

    I would ask, though, why you are using the g modifier? Is this all part
    of a larger string that there might be several such matches on? If so
    then what happens if there is a line strawberry-banana in there? You
    haven't anchored the banana portion.

    I would also ask why you escaped the period and slash in your replacement
    pattern? In replacement patterns, period and slash stand for themselves
    [unless one of them happens to be the pattern delimeter.]

    To confirm: the output you would expect over those lines would be

    banana\n
    \t./apple\n

    ??
    --
    Aleph sub {Aleph sub null} little, Aleph sub {Aleph sub one} little,
    Aleph sub {Aleph sub two} little infinities...
     
    Walter Roberson, Jun 3, 2004
    #2
    1. Advertising

  3. On Thu, 03 Jun 2004 17:22:33 +0000, Walter Roberson wrote:

    > In article <>,
    > Wojtek Michalik <> wrote:
    > :Can anybody give me en example of working regex
    > :that will match sample text given below:
    > :____________
    > :banana\n
    > :\tapple\n
    > :____________
    > :\t=tab and \n=nweline
    > :I have been trying to match it by:
    > :perl -pi -e 's#(banana\n\t)(apple)#$1\.\/$2#g' with modifiers like m s ms
    > :nothing worked.
    >
    > You haven't really defined what your pattern is.
    >
    > I would ask, though, why you are using the g modifier?


    Because I am a perl newbie. Don't know much about it.

    >Is this all part
    > of a larger string that there might be several such matches on? If so
    > then what happens if there is a line strawberry-banana in there?


    Can be, I will deal with it. The only -I hope - problem I have, is newline inside my pattern

    > You haven't anchored the banana portion.


    Why is it so important ???

    > I would also ask why you escaped the period and slash in your replacement
    > pattern? In replacement patterns, period and slash stand for themselves
    > [unless one of them happens to be the pattern delimeter.]


    Often don't remember the obove - that's why.

    > To confirm: the output you would expect over those lines would be
    >
    > banana\n
    > \t./apple\n
    >
    > ??

    ------exactly----------
     
    Wojtek Michalik, Jun 3, 2004
    #3
  4. Wojtek Michalik

    John Bokma Guest

    Wojtek Michalik wrote:

    > On Thu, 03 Jun 2004 17:22:33 +0000, Walter Roberson wrote:


    >>I would ask, though, why you are using the g modifier?

    >
    > Because I am a perl newbie. Don't know much about it.


    So that means you just try something without actually reading what it
    means? You saw somewhere g and though, well, let's try it.

    Regexpen are not made on trail and error basis but with careful thinking.

    --
    John MexIT: http://johnbokma.com/mexit/
    personal page: http://johnbokma.com/
    Experienced Perl programmer available: http://castleamber.com/
    Happy Customers: http://castleamber.com/testimonials.html
     
    John Bokma, Jun 3, 2004
    #4
  5. Wojtek Michalik

    Guest

    "Wojtek Michalik" <> wrote:
    > Can anybody give me en example of working regex
    > that will match sample text given below:
    > ____________
    > banana\n
    > \tapple\n
    > ____________


    When including literal text in a post, quote it the way you would
    quote it in Perl. Otherwise it is ambiguous. It appears that there are
    two newlines between "banana" and "apple", because "\n" represents one
    newline, and the actual newline (which causes them to be on separate lines)
    represents another newline. Well, assuming the above is in double-quotish
    circumstances, which reading further, I suspect it isn't.

    > \t=tab and \n=nweline


    Maybe it does, maybe it doesn't. Since it looks like you are using
    the the i switch, I'm guessing that in fact \t represents '\t' and \n
    represents '\n'.


    > I have been trying to match it by:
    > perl -pi -e 's#(banana\n\t)(apple)#$1\.\/$2#g' with modifiers like m s ms


    Dealing with shell special characters and escaping, as well as perl special
    characters and escaping, is a maddening proposition. If you can't make
    your -e command-liner work, turn it into a real script before debugging.
    Once you get your script to work, then you try turning it back into a
    one-liner.

    Also, simplify the substitution into a simple match. Once you get that
    to work, you can go back to the substitution.

    Finally, have you tried simply printing the input with so you can see what
    it actually looks like?

    Xho

    --
    -------------------- http://NewsReader.Com/ --------------------
    Usenet Newsgroup Service $9.95/Month 30GB
     
    , Jun 3, 2004
    #5
  6. Wojtek Michalik

    Guest

    "Wojtek Michalik" <> wrote:
    > Can anybody give me en example of working regex
    > that will match sample text given below:
    > ____________
    > banana\n
    > \tapple\n
    > ____________


    When including literal text in a post, quote it the way you would
    quote it in Perl. Otherwise it is ambiguous. It appears that there are
    two newlines between "banana" and "apple", because "\n" represents one
    newline, and the actual newline (which causes them to be on separate lines)
    represents another newline. Well, assuming the above is in double-quotish
    circumstances, which reading further, I suspect it isn't.

    > \t=tab and \n=nweline


    Maybe it does, maybe it doesn't. Since it looks like you are using
    the the i switch, I'm guessing that in fact \t represents '\t' and \n
    represents '\n'.

    > I have been trying to match it by:
    > perl -pi -e 's#(banana\n\t)(apple)#$1\.\/$2#g' with modifiers like m s ms


    Dealing with shell special characters and escaping, as well as perl special
    characters and escaping, is a maddening proposition. If you can't make
    your -e command-liner work, turn it into a real script before debugging.
    Once you get your script to work, then you try turning it back into a
    one-liner.

    Also, simplify the substitution into a simple match. Once you get that
    to work, you can go back to the substitution.

    Finally, have you tried simply printing the input without modification so
    you can see what it actually looks like?

    Xho

    --
    -------------------- http://NewsReader.Com/ --------------------
    Usenet Newsgroup Service $9.95/Month 30GB
     
    , Jun 3, 2004
    #6
  7. Wojtek Michalik <> wrote:

    > Can anybody give me en example of working regex
    > that will match sample text given below:
    > ____________
    > banana\n
    > \tapple\n
    > ____________
    > \t=tab and \n=nweline
    > I have been trying to match it by:
    > perl -pi -e 's#(banana\n\t)(apple)#$1\.\/$2#g' with modifiers like
    > m s ms nothing worked.
    > I use perl version 5.8 on linux


    In addition to the other advice you've been given, a closer look at the
    -p option could be beneficial. You're reading one line at a time (I
    don't see $/ being redefined anywhere) and trying to match over
    multiple lines. Somehow I'm not surprised that success is eluding you.
    :)
     
    David K. Wall, Jun 3, 2004
    #7
  8. Wojtek Michalik

    Joe Smith Guest

    Wojtek Michalik wrote:

    > perl -pi -e 's#(banana\n\t)(apple)#$1\.\/$2#g' with modifiers like m s ms


    That won't work because using -p reads the file one line at a time.
    This means that is stops at each \n. The first time through, $_ will
    have "banana\n" and the second time $_ will have "\tapple\n".
    In neither case will \n and \t be consecutive characters.

    Since the pattern you're looking for spans lines, you'll need to
    slurp the entire file into a scalar variable before looking for it.

    Use the command
    perldoc perlvar
    and look for the section on $INPUT_RECORD_SEPARATOR for an example
    for using slurp mode.
    -Joe
     
    Joe Smith, Jun 4, 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. Greg Hurrell
    Replies:
    4
    Views:
    181
    James Edward Gray II
    Feb 14, 2007
  2. Mikel Lindsaar
    Replies:
    0
    Views:
    546
    Mikel Lindsaar
    Mar 31, 2008
  3. Joao Silva
    Replies:
    16
    Views:
    407
    7stud --
    Aug 21, 2009
  4. Uldis  Bojars
    Replies:
    2
    Views:
    214
    Janwillem Borleffs
    Dec 17, 2006
  5. Matìj Cepl

    new RegExp().test() or just RegExp().test()

    Matìj Cepl, Nov 24, 2009, in forum: Javascript
    Replies:
    3
    Views:
    203
    Matěj Cepl
    Nov 24, 2009
Loading...

Share This Page