簡體   English   中英

正則表達式換行符

[英]Regex New Line No Match

我試圖在屏幕上刮一些HTML,並且在跨新行(在.Net中)匹配時遇到問題

這是文本:

<td class=abc><span class=label>XXX</span></td>
<td class=def><span class=field>YYY</span></td>

我正在嘗試將YYY與該公式匹配

<td class=abc><span class=label>XXX</span></td>\n<td class=def><span class=field>(.*)</span></td>

我已經\\n分界線了,但是不匹配...有什么想法嗎?

[編輯]

添加了\\r\\n而不只是\\ n,它可以工作。

您需要為正則表達式使用多行修飾符m 在VB.NET中,它作為正則表達式的選項提供。 但是,您還需要使用反斜杠來轉義所有正斜杠:

<td class=abc><span class=label>XXX<\/span><\/td>\n<td class=def><span class=field>(.*)<\/span><\/td>

但是請注意,正則表達式是解析HTML的一種非常差的方法-大多數語言中都有HTML解析器做得更好。

而且您的正則表達式非常詳細,因此很脆弱; 額外的空間將導致它失敗。

請注意,在Windows中,換行符通常使用回車和換行符\\r\\n

這是提供“ Multiline選項的示例:

Dim rex As New Regex("\bsomething\b", RegexOptions.MultiLine)

正則表達式選項 :MSDN

Here I write perl code but in if condition ,used \n new line character not match  



  #!/usr/bin/perl
    use strict;
    #use warnings;
    use Cwd;
    use File::Basename;
    use File::Copy;

    my $path=getcwd;    
    #print $path."\n";
    opendir(INP, "$path\/");
    my @out = grep(/.(xml)$/,readdir(INP));
    close INP;
    #print @out;
    open(F6, ">Log.txt");
    foreach my $f1(@out)
    {
        open(FF, "<$path\/$f1") or die "Cannot open file: $out[0]";
        my $data1 = join("", <FF>); 
        my @FILE_KA_ARRAY = split(/\n/, $data1);
        my $file_ka_len = @FILE_KA_ARRAY;
        #print F6 $file_ka_len."\n";
        #print F6 $f."\t".$file_ka_len."\n";
        print F6 $f1."\n";
        for(my $x=1; $x<$file_ka_len; $x++)
        {   
            my $y=$x+1;
            my $temp_file_arr = ""; 
            $temp_file_arr = $FILE_KA_ARRAY[$x];
        #print F6 $temp_file_arr."\t$x\n";
        my $temp1=$temp_file_arr;
    if($temp1=~m#(<list .*? depth="(\d+)">)\n?(<list .*? depth="(\d+)">)#gs)
    {
    my $list3=$1;
    print F6 "\t\t\t\t\t\t\t\t".$y."\t\t".$list3."\n";
    }
    }
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM