Got more questions? Find advice on: ASP | SQL | XML | Windows
in Search
Welcome to RegexAdvice Sign in | Join | Help

Identify lines that begin with > in a string

Last post 11-08-2007, 7:09 PM by Rush2112. 7 replies.
Sort Posts: Previous Next
  •  11-06-2007, 8:09 PM 36297

    Identify lines that begin with > in a string

    I'm trying to find all the lines that begin with > and added bbcode around them and ignore all lines that do not begin with >

    Sample data:

    Line 1
    >Line 2
    >Line 3
    Line 4

    >Line 5

    Last line

    The desired output is:

    Line 1
    quote>Line 2
    >Line 3/quote
    Line 4

    quote>Line 5/quote

    Last line

     

    I've come close, but the formatting just doesn't come out correctly.  The /quote after 'Line 3' appears after 'Line 4'
    This is what I was trying to use: ^(>\s*)+.*/m

    Tried cracking this nut for about a month now, but haven't quite got it yet.

    Thanks,

    Rush
     

  •  11-07-2007, 10:02 AM 36311 in reply to 36297

    Re: Identify lines that begin with > in a string

    match on '/(>.*?)(\r\n[^>]|$)/s'

    replace with 'quote$1/quote$2'


  •  11-07-2007, 11:23 AM 36312 in reply to 36311

    Re: Identify lines that begin with > in a string

    ddrudik,

    Your suggestion doesn't handle the consecutive lines starting with '>' being one quote like lines 2 and 3 in the sample.

     

    Rush,

    Try pattern

    (?:^>.+)(?:\n^>.+)*

    with replacement string

    quote$0/quote
     


    Michael

    "In theory, theory and practice are the same. In practice, they are not."
    Albert Einstein
  •  11-07-2007, 12:35 PM 36315 in reply to 36312

    Re: Identify lines that begin with > in a string

    My testing shows different results, given the code:

     <?php
    $string = <<<EOF
    Line 1
    >Line 2
    >Line 3
    Line 4

    >Line 5

    Last line
    EOF;
    $pattern '/(>.*?)(\r\n[^>]|$)/s';
    $repl 'quote$1/quote$2';
    echo preg_replace($pattern$repl$string );
    ?>
     

    Results in:

      Line 1
      quote>Line 2
      >Line 3/quote
      Line 4

      quote>Line 5/quote

      Last line

    The pattern (?:^>.+)(?:\n^>.+)* does not match the source for me.


  •  11-07-2007, 6:11 PM 36325 in reply to 36315

    Re: Identify lines that begin with > in a string

    Maybe it's a flavor thing.  Testing on http://rereplace.com

    Input

    Line 1
    >Line 2
    >Line 3
    Line 4

    >Line 5

    Last line

    Test 1:  pattern (>.*?)(\r\n[^>]|$)

               replacement string = quote$1/quote$2

    Result :

    Line 1
    quote>Line 2/quote
    quote>Line 3/quote
    Line 4

    quote>Line 5/quote

    Last line

     

    Test 2:  pattern (?:^>.+)(?:\n^>.+)*

    replacement string = quote$0/quote

    Result

    Line 1
    quote>Line 2
    >Line 3/quote
    Line 4

    quote>Line 5/quote

    Last line
     


    Michael

    "In theory, theory and practice are the same. In practice, they are not."
    Albert Einstein
  •  11-07-2007, 8:34 PM 36331 in reply to 36325

    Re: Identify lines that begin with > in a string

  •  11-08-2007, 12:43 AM 36336 in reply to 36331

    Re: Identify lines that begin with > in a string

    Ah I see what happening, first off nice test site.

    My regex needs multiline option on. My  test platforms had it on by default. 


    Michael

    "In theory, theory and practice are the same. In practice, they are not."
    Albert Einstein
  •  11-08-2007, 7:09 PM 36353 in reply to 36336

    Re: Identify lines that begin with > in a string

    Well, I tried: (?:^>.+)(?:\n^>.+)*
    and it is working great on my site.

    I really do appreciate the time and suggestions from everyone!

     
    Thanks!

    Rush


     

View as RSS news feed in XML