You could try the following as it seems to cope with spaces between the b and it's newline and with b with an immediate newline by making the spaces optional and also making a character class that excludes newline, unlike \s.

Set up some text to try and set up the regular expression.

#!/usr/local/bin/perl # use strict; use warnings; our @tryThese = ( "a 10\nb a2 s2\nc 30", "a 10\nb\nc 30", "a 10\nb \nc 30", "a 10\nb a2 s2 \nc 30"); our $c; our $rxAfterB = qr{(?m)^b[\x20\x09]*([^\n]*)};

Loop over @tryThese printing out what we are testing, doing the match then showing the result.

foreach my $a (@tryThese) { print "\n\$a contains ...\n"; { local $" = "<--\n"; print "@{[split /\n/, $a]}\n"; } ($c) = $a =~ /$rxAfterB/; print "c is -->$c<--\n\n"; }

When run this produces

$a contains ... a 10<-- b a2 s2<-- c 30 c is -->a2 s2<-- $a contains ... a 10<-- b<-- c 30 c is --><-- $a contains ... a 10<-- b <-- c 30 c is --><-- $a contains ... a 10<-- b a2 s2 <-- c 30 c is -->a2 s2 <--

I hope this is of use.

Cheers,

JohnGG

Update: The slashes around the compiled regular expression in the line ($c) = $a =~ /$rxAfterB/; are superfluous. The line should read

($c) = $a =~ $rxAfterB;

JohnGG


In reply to Re^2: regexp need a match characters and spaces by johngg
in thread regexp need a match characters and spaces by jeanluca

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.