regex: match all including new line

facebooktwittergoogle_plusredditpinterestlinkedinmailby feather

I was looking for a way to match all characters plus new line and using “.” is not solely the answer because:

The dot matches a single character, without caring what that character is. The only exception are newline characters. In all regex flavors discussed in this tutorial, the dot will not match a newline character by default. So by default, the dot is short for the negated character class [^\n] (UNIX regex flavors) or [^\r\n](Windows regex flavors).

source: http://www.regular-expressions.info/dot.html

So I found some answers on the references below by using the \s

\s
Matches any whitespace character; this is equivalent to the class [ \t\n\r\f\v].

sources:
http://www.amk.ca/python/howto/regex/

http://www.phpro.org/tutorials/Introduction-to-PHP-Regex.html

So combining it all example showed below:

<?
$str = ‘you \n are \n good’;
preg_match(‘/(.*)/s’, $str, $match);

?>

[additions] After testing several examples I found out that S is not enough for the example below, so instead I added the U pattern modifier so it would be like┬ápreg_match(‘/div(.*)<\/div>/sU’, $str, $match);

$a = “
<div>
<p>aaa</p>
</div>
<div>
<p>bbb</p>
</div>
<div>
<p>ccc</p>
</div>
“;

You can also check regular expressions list here.

facebooktwittergoogle_plusredditpinterestlinkedinmailby feather

4 Responses to “regex: match all including new line

  • Thanks! You save the day :)

    a search on stackoverflow didn’t return any solution using /sU :)

  • This helped me out, PHP’s solution was different than the other language solutions out there. Thanks!

  • thanks bro.. its work. solve my problem

  • Hi

    I need help on a regex
    Here is my code:

    #!/usr/bin/perl

    my $file =”rejets.txt”;

    open (FILE, $file) or die “could not open file!”;
    while () {
    $line =$_;
    if ($line =~ m/^\[app/) {
    print $line;
    }
    }

    Out_put of my code is " [app:tac/toc]”

    Out_put wanted is :

    [app:tac/toc]
    mode=job
    heure_debut=00:00:00
    heure_fin=23:30:00
    commentaire=
    type_periodicite=periodique
    cyclique=non
    cycle=00:00:00
    periodicite=Journaliere
    machine=
    queue=queue_ksh
    utilisateur=vtomtech
    date=yoyo
    status=TERMINE
    fstatus=A_VENIR
    derniere_exec=11-01-2013 09:22:12
    derniere_exec_fin=14-01-2013 09:34:54
    derniere_date=08-10-2012
    derniere_stat=08-10-2012
    ne_pas_deplanifier=o
    attendre_avant_deplanification=n
    nombre_de_jobs=
    dfl_node=
    link_diag=1/1
    geometrie=
    sbgclr=Blue
    slblclr=White
    sdrwlbl=2
    sshape=1
    planning

    Here is the text:

    [app:tac/toc]
    mode=job
    heure_debut=00:00:00
    heure_fin=23:30:00
    commentaire=
    type_periodicite=periodique
    cyclique=non
    cycle=00:00:00
    periodicite=Journaliere
    machine=
    queue=queue_ksh
    utilisateur=vtomtech
    date=yoyo
    status=TERMINE
    fstatus=A_VENIR
    derniere_exec=11-01-2013 09:22:12
    derniere_exec_fin=14-01-2013 09:34:54
    derniere_date=08-10-2012
    derniere_stat=08-10-2012
    ne_pas_deplanifier=o
    attendre_avant_deplanification=n
    nombre_de_jobs=
    dfl_node=
    link_diag=1/1
    geometrie=
    sbgclr=Blue
    slblclr=White
    sdrwlbl=2
    sshape=1
    planning

    [planning:]
    jour_semaine=C
    semaine_mois=O
    mois_annee=O
    verifie_planning=oui
    verifie_formule=non

    Any help please thanks

Leave a Reply