Regex for "not any words(lines) preceeded by spaces"

Discussion in 'Perl Misc' started by Mark Clements, May 18, 2004.

  1. Edward Wijaya wrote:
    > Hi,
    >
    > [^\s.] or [^\s[a-zA-z]] doesn't seem to work.
    > Anything wrong with my regex?

    [^\s.] will match non-whitespace or non-anything (^ inside a character
    class negates the class). I'll assume that you are trying to anchor to
    start-of line, though your spec doesn't mention it. You spec refers to
    spaces and not whitespace, though this time I'll assume you mean spaces.

    How about:
    ^ +\W+

    though without any test data or expected results it's difficult to see
    exactly what you're after.

    Mark
    Mark Clements, May 18, 2004
    #1
    1. Advertising

  2. Mark Clements wrote:
    > [^\s.] will match non-whitespace or non-anything


    No. A dot is not special within brackets. It matches any
    non-whitespace character except a dot.

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Hjalmarsson, May 18, 2004
    #2
    1. Advertising

  3. Gunnar Hjalmarsson wrote:
    > Mark Clements wrote:
    >
    >> [^\s.] will match non-whitespace or non-anything

    >
    >
    > No. A dot is not special within brackets. It matches any non-whitespace
    > character except a dot.
    >

    er - yeah. schoolboy error :)
    Mark Clements, May 18, 2004
    #3
  4. Mark Clements

    Anno Siegel Guest

    Edward Wijaya <> wrote in comp.lang.perl.misc:
    > Hi,
    >
    > [^\s.] or [^\s[a-zA-z]] doesn't seem to work.
    > Anything wrong with my regex?


    Hard to say before *you* say what exactly you expect it to do. The
    statement in your subject is ambiguous (and you should have re-stated
    it in the body of your text).

    Meanwhile it would be a good idea to acquaint yourself with the
    function of character classes "[...]" in regular expressions.
    Those you are using above don't make much sense. In particular
    you don't seem to understand that a character class matches a
    single character, not many. Also, "^" has a different meaning
    in character classes (negation) and outside (anchor to the beginning
    the string).

    Anno
    Anno Siegel, May 18, 2004
    #4
  5. Mark Clements

    Anno Siegel Guest

    Re: My Example - Regex for "not any words(lines) preceeded by spaces"

    Edward Wijaya <> wrote in comp.lang.perl.misc:

    [please don't to-post]

    > This is the example of the input (verbatim).
    >
    > AGATTAGT
    > GCATTAGT
    > AGGGTAGT
    > GGAGTAGG
    > A C G T Info Ptn Pattern
    > TOTAL INFORMATION CONTENT
    >
    >
    > I just want to remove the last two lines (that comes
    > with spaces before it).


    grep /^\S/, @input;

    [fullquote snipped]

    Anno
    Anno Siegel, May 18, 2004
    #5
  6. Hi,

    [^\s.] or [^\s[a-zA-z]] doesn't seem to work.
    Anything wrong with my regex?

    Thanks.

    Regards
    Edward WIJAYA
    SINGAPORE


    --
    Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
    Edward Wijaya, May 18, 2004
    #6
  7. Re: My Example - Regex for "not any words(lines) preceeded by spaces"

    -berlin.de (Anno Siegel) wrote in message news:<c8d3s3$es9$-Berlin.DE>...
    > Edward Wijaya <> wrote in comp.lang.perl.misc:
    >
    > [please don't to-post]
    >
    > > This is the example of the input (verbatim).
    > >
    > > AGATTAGT
    > > GCATTAGT
    > > AGGGTAGT
    > > GGAGTAGG
    > > A C G T Info Ptn Pattern
    > > TOTAL INFORMATION CONTENT
    > >
    > >
    > > I just want to remove the last two lines (that comes
    > > with spaces before it).

    >
    > grep /^\S/, @input;
    >
    > [fullquote snipped]
    >
    > Anno


    Here is another method.

    perl -ne 'print if /^[A-Z]+/' a.out


    perl -ne 'print if /^\w+/' a.out

    -sundaram
    Sundaram Ramasamy, May 18, 2004
    #7
  8. Mark Clements

    Paul Lalli Guest

    Re: My Example - Regex for "not any words(lines) preceeded by spaces"

    On Tue, 18 May 2004, Sundaram Ramasamy wrote:

    > > Edward Wijaya <> wrote in comp.lang.perl.misc:
    > >
    > > [please don't to-post]
    > >
    > > > This is the example of the input (verbatim).
    > > >
    > > > AGATTAGT
    > > > GCATTAGT
    > > > AGGGTAGT
    > > > GGAGTAGG
    > > > A C G T Info Ptn Pattern
    > > > TOTAL INFORMATION CONTENT
    > > >
    > > >
    > > > I just want to remove the last two lines (that comes
    > > > with spaces before it).

    >
    > Here is another method.
    >
    > perl -ne 'print if /^[A-Z]+/' a.out
    >
    >
    > perl -ne 'print if /^\w+/' a.out
    >


    Neither of those meet the specs the OP specified. The requirements were
    to print lines that do not begin with spaces. Your methods print only
    those lines that begin with Capital letters and letters, numbers, &
    underscores (respectively). Granted, the input he chose to show contains
    only capital letters as the first non-whitespace. That does not mean,
    however, that *all* input the OP will use meets that format, nor should it
    be assumed his input will never change.

    perl -ne 'print if /^\S/' a.out

    Paul Lalli
    Paul Lalli, May 18, 2004
    #8
  9. My Example - Regex for "not any words(lines) preceeded by spaces"

    Hi,
    This is the example of the input (verbatim).

    AGATTAGT
    GCATTAGT
    AGGGTAGT
    GGAGTAGG
    A C G T Info Ptn Pattern
    TOTAL INFORMATION CONTENT


    I just want to remove the last two lines (that comes
    with spaces before it).

    Regards
    Edward WIJAYA


    On 18 May 2004 11:01:45 GMT, Anno Siegel
    <-berlin.de> wrote:

    > Edward Wijaya <> wrote in comp.lang.perl.misc:
    >> Hi,
    >>
    >> [^\s.] or [^\s[a-zA-z]] doesn't seem to work.
    >> Anything wrong with my regex?

    >
    > Hard to say before *you* say what exactly you expect it to do. The
    > statement in your subject is ambiguous (and you should have re-stated
    > it in the body of your text).
    >
    > Meanwhile it would be a good idea to acquaint yourself with the
    > function of character classes "[...]" in regular expressions.
    > Those you are using above don't make much sense. In particular
    > you don't seem to understand that a character class matches a
    > single character, not many. Also, "^" has a different meaning
    > in character classes (negation) and outside (anchor to the beginning
    > the string).
    >
    > Anno




    --
    Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
    Edward Wijaya, May 18, 2004
    #9
  10. Re: My Example - Regex for "not any words(lines) preceeded by spaces"

    Paul Lalli wrote:
    > On Tue, 18 May 2004, Sundaram Ramasamy wrote:
    >
    >
    >>>Edward Wijaya <> wrote in comp.lang.perl.misc:
    >>>
    >>>[please don't to-post]
    >>>
    >>>
    >>>>This is the example of the input (verbatim).
    >>>>
    >>>>AGATTAGT
    >>>>GCATTAGT
    >>>>AGGGTAGT
    >>>>GGAGTAGG
    >>>> A C G T Info Ptn Pattern
    >>>> TOTAL INFORMATION CONTENT
    >>>>
    >>>>
    >>>>I just want to remove the last two lines (that comes
    >>>>with spaces before it).

    >>
    >>Here is another method.
    >>
    >> perl -ne 'print if /^[A-Z]+/' a.out
    >>
    >>
    >> perl -ne 'print if /^\w+/' a.out
    >>

    >
    >
    > Neither of those meet the specs the OP specified.


    > perl -ne 'print if /^\S/' a.out


    Nitpicking (and showing that TMTOWTDI):
    Your solution does not meet the specs of the OP either:

    perl -ne 'print unless /^\s+/' file

    !!Josef B-{)
    --
    Josef Möllers (Pinguinpfleger bei FSC)
    If failure had no penalty success would not be a prize
    -- T. Pratchett
    Josef Moellers, May 19, 2004
    #10
  11. Mark Clements

    Joe Smith Guest

    Re: My Example - Regex for "not any words(lines) preceeded by spaces"

    Josef Moellers wrote:

    > Paul Lalli wrote:


    >> perl -ne 'print if /^\S/' a.out

    >
    > Nitpicking (and showing that TMTOWTDI):
    > Your solution does not meet the specs of the OP either:
    >
    > perl -ne 'print unless /^\s+/' file


    There is no difference in output between
    perl -ne 'print unless /^\s+/' file
    and
    perl -ne 'print unless /^\s/' file

    Since the latter is the same as
    perl -ne 'print if /^\S/' file
    so why do you say it 'does not meet the specs'?

    -Joe
    Joe Smith, Jun 5, 2004
    #11
    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. =?Utf-8?B?SmViQnVzaGVsbA==?=

    Is ASP Validator Regex Engine Same As VS2003 Find Regex Engine?

    =?Utf-8?B?SmViQnVzaGVsbA==?=, Oct 22, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    695
    =?Utf-8?B?SmViQnVzaGVsbA==?=
    Oct 22, 2005
  2. Rick Venter

    perl regex to java regex

    Rick Venter, Oct 29, 2003, in forum: Java
    Replies:
    5
    Views:
    1,619
    Ant...
    Nov 6, 2003
  3. Replies:
    2
    Views:
    594
  4. Xah Lee
    Replies:
    1
    Views:
    937
    Ilias Lazaridis
    Sep 22, 2006
  5. Replies:
    3
    Views:
    746
    Reedick, Andrew
    Jul 1, 2008
Loading...

Share This Page