How to find the end of a word in perl..

Discussion in 'Perl Misc' started by Sooraj S, Jun 29, 2010.

  1. Sooraj S

    Sooraj S Guest

    Hi,

    My data file :
    -------------------
    // abc def
    //
    // abc dser
    //
    ----------------------
    i want to remove the lines 2 and 4 from the file.

    check criteria : line should start with // and one or more spaces only
    as the other characters.

    i tried this .. if (/^\/\/(\s)*/ ... but it shows entire lines..

    How to do it using regular expressions ? Is there any way to find the
    end of a word using regular expressions ?
    Sooraj S, Jun 29, 2010
    #1
    1. Advertising

  2. Sooraj S <> wrote:
    >Hi,
    >
    >My data file :
    >-------------------
    >// abc def
    >//
    >// abc dser
    >//
    >----------------------
    >i want to remove the lines 2 and 4 from the file.
    >
    >check criteria : line should start


    ^

    > with //


    //

    >and one or more spaces only


    + (that is a space character followed by a plus sign)

    >as the other characters.


    $

    So the whole RE becomes
    (^// +$)

    >i tried this .. if (/^\/\/(\s)*/ ... but it shows entire lines..
    >
    >How to do it using regular expressions ?


    You are plenking.

    >Is there any way to find the
    >end of a word using regular expressions ?


    Sure, from 'perldoc perlre':

    Assertions
    Perl defines the following zero-width assertions:
    \b Match a word boundary

    But what does that question have to do with your earlier problem
    statement?

    jue
    Jürgen Exner, Jun 29, 2010
    #2
    1. Advertising

  3. Sooraj S

    Steve C Guest

    Jürgen Exner wrote:
    > Sooraj S <> wrote:
    >> How to do it using regular expressions ?

    >
    > You are plenking.
    >


    Thanks for the new word.
    Steve C, Jun 29, 2010
    #3
  4. Sooraj S

    Justin C Guest

    On 2010-06-29, Sooraj S <> wrote:
    > Hi,
    >
    > My data file :
    > -------------------
    > // abc def
    > //
    > // abc dser
    > //
    > ----------------------
    > i want to remove the lines 2 and 4 from the file.
    >
    > check criteria : line should start with // and one or more spaces only
    > as the other characters.
    >
    > i tried this .. if (/^\/\/(\s)*/ ... but it shows entire lines..
    >
    > How to do it using regular expressions ? Is there any way to find the
    > end of a word using regular expressions ?


    I think you want a '+' where you have '*'. '*' means 'any number of
    times including 0'. '+' means 'at least once'.

    However, that will still match the lines that you want to keep. You have
    the '^' to match at the beginning of the string. If only there was
    something to match the end of the string...

    Justin.

    PS Regex word boundary = \b
    --
    Justin C, by the sea.
    Justin C, Jun 29, 2010
    #4
  5. Steve C <> wrote:
    >Jürgen Exner wrote:
    >> Sooraj S <> wrote:
    >>> How to do it using regular expressions ?

    >>
    >> You are plenking.

    >
    >Thanks for the new word.


    Nothing new about it: http://en.wikipedia.org/wiki/Plenk

    jue
    Jürgen Exner, Jun 29, 2010
    #5
  6. Sooraj S

    C.DeRykus Guest

    On Jun 29, 3:42 am, Sooraj S <> wrote:
    > Hi,
    >
    > My data file :
    > -------------------
    > // abc def
    > //
    > //    abc dser
    > //
    > ----------------------
    > i want to remove the lines 2 and 4 from the file.
    >
    > check criteria : line should start with // and one or more spaces only
    > as the other characters.
    >
    > i tried this ..  if (/^\/\/(\s)*/ ... but it shows entire lines..
    >
    > How to do it using regular expressions ? Is there any way to find the
    > end of a word using regular expressions ?



    If non-whitespace is the secondary criterion for validity:


    print if m{^ // \s+ (?=.*?\S) }x; # plenken/klempen frei

    --
    Charles DeRykus
    C.DeRykus, Jun 30, 2010
    #6
  7. Sooraj S

    C.DeRykus Guest

    On Jun 29, 10:07 pm, "C.DeRykus" <> wrote:
    > On Jun 29, 3:42 am, Sooraj S <> wrote:
    >
    >
    >
    > > Hi,

    >
    > > My data file :
    > > -------------------
    > > // abc def
    > > //
    > > //    abc dser
    > > //
    > > ----------------------
    > > i want to remove the lines 2 and 4 from the file.

    >
    > > check criteria : line should start with // and one or more spaces only
    > > as the other characters.

    >
    > > i tried this ..  if (/^\/\/(\s)*/ ... but it shows entire lines..

    >
    > > How to do it using regular expressions ? Is there any way to find the
    > > end of a word using regular expressions ?

    >
    > If non-whitespace is the secondary criterion for validity:
    >
    > print if m{^ // \s+ (?=.*?\S) }x;  # plenken/klempen frei
    >


    or lookahead free too:

    print if m{^ // \s+ \S }x;


    --
    Charles DeRykus
    C.DeRykus, Jun 30, 2010
    #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. Shea Martin
    Replies:
    1
    Views:
    163
    Rodrigo Bermejo
    Jan 15, 2007
  2. Mohit Sindhwani
    Replies:
    5
    Views:
    184
    Mohit Sindhwani
    Oct 27, 2008
  3. Tony
    Replies:
    2
    Views:
    377
  4. Lim kiang Leng
    Replies:
    3
    Views:
    206
    Ben Morrow
    Jul 18, 2004
  5. Guest
    Replies:
    4
    Views:
    287
    Guest
    May 12, 2006
Loading...

Share This Page