FAQ
Hi I am trying to capture only the first occurence of a charcter
followed by any word ie: in this sample text

!this is a first level heading
!!this is a 2nd level heading

I only want to find the first item and not the second

current code

$line=~s/\!(^\![A-Z][a-z].+)/$1/gc;

it either doesnt capture any ! or takes out the first ! when there is a double

any ideas

Search Discussions

  • Paul Lalli at Sep 27, 2007 at 1:05 pm

    On Sep 27, 8:58 am, redt...@googlemail.com (Mike Martin) wrote:
    Hi I am trying to capture only the first occurence of a charcter
    followed by any word ie: in this sample text

    !this is a first level heading
    !!this is a 2nd level heading

    I only want to find the first item and not the second

    current code

    $line=~s/\!(^\![A-Z][a-z].+)/$1/gc;
    This says to search for an exclamation mark, followed by the beginning
    of the string (which makes no sense, of course), followed by another
    exclamation mark, followed by a capital letter, a lowercase letter,
    and then one or more of anything. And replace all of that with
    everything except the first exclamation.
    it either doesnt capture any ! or takes out the first ! when there is a double
    No it doesn't. There is no way this pattern match ever succeeds.
    It's not possible to find some character and *then* the beginning of
    the string.

    Provide sample input and output of what it is you're trying to do.
    Your english description of the problem is confusing at best.

    Paul Lalli
  • Rob Dixon at Sep 27, 2007 at 1:15 pm

    Mike Martin wrote:
    Hi I am trying to capture only the first occurence of a charcter
    followed by any word ie: in this sample text

    !this is a first level heading
    !!this is a 2nd level heading

    I only want to find the first item and not the second

    current code

    $line=~s/\!(^\![A-Z][a-z].+)/$1/gc;

    it either doesnt capture any ! or takes out the first ! when there is a double

    any ideas
    Hi Mike. I'm confused as to what you're trying to do, as your regex
    doesn't make sense and is a subtitution rather than a pattern match. My
    best guess is that you want to elevate all second-level headings to first-
    level ones? Maybe something like

    $line =~ s/^!(![a-z].+)/$1/gi;

    does what you need.

    HTH,

    Rob
  • John W. Krahn at Sep 27, 2007 at 2:17 pm

    Rob Dixon wrote:

    Mike Martin wrote:
    $line=~s/\!(^\![A-Z][a-z].+)/$1/gc;
    $line =~ s/^!(![a-z].+)/$1/gi;
    You are both using an anchored pattern with the /g option which makes no sense
    unless you are also using the /m option.


    John
    --
    Perl isn't a toolbox, but a small machine shop where you
    can special-order certain sorts of tools at low cost and
    in short order. -- Larry Wall
  • Rob Dixon at Sep 27, 2007 at 7:18 pm

    Mike Martin wrote:
    On 27/09/2007, Rob Dixon wrote:

    Mike Martin wrote:
    Hi I am trying to capture only the first occurence of a charcter
    followed by any word ie: in this sample text

    !this is a first level heading
    !!this is a 2nd level heading

    I only want to find the first item and not the second

    current code

    $line=~s/\!(^\![A-Z][a-z].+)/$1/gc;

    it either doesnt capture any ! or takes out the first ! when there is a double

    any ideas
    Hi Mike. I'm confused as to what you're trying to do, as your regex
    doesn't make sense and is a subtitution rather than a pattern match. My
    best guess is that you want to elevate all second-level headings to first-
    level ones? Maybe something like

    $line =~ s/^!(![a-z].+)/$1/gi;

    does what you need.
    nope - I am trying to find only strings that start with ! and not
    those starting with !!
    Then you want

    $line =~ s/^!([a-z].+)/$1/i;

    or, better:

    $line =~ s/^!(?=[^!])//;

    Rob

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupbeginners @
categoriesperl
postedSep 27, '07 at 12:58p
activeSep 27, '07 at 7:18p
posts5
users4
websiteperl.org

People

Translate

site design / logo © 2021 Grokbase