2017-11-24 3 views
1

私はSASで働いており、この文字列で正しく動作するperl reg exを調べようとしています(srcは文字列、txtは行I ...たいと出力ラインの任意の数があるかもしれません)以下Perl Reg Ex式 - 後にパターンとテキストを抽出する

src='01/04/2017 03:45:32 Some Comment - abc 05/04/2017 16:32:41 Some other Comment 06/07 at something' 
txt='01/04/2017 03:45:32 Some Comment - abc' 
txt='05/04/2017 16:32:41 Some other Comment 06/07 at something' 

は、私はこれを試してみて、試みること使用していたSASコードです...

data _null_; 
    ExpressionID = PRXPARSE('/(\d{2}\/\d{2}\/\d{4}) (\s) (\d{2}\:\d{2}\:\d{2}) /xio'); 
    text = '01/04/2017 03:45:32 Some Comment - abc 05/04/2017 16:32:41 Some other Comment 06/07 at something'; 

    start = 1; 
    stop = length(text); 

    /* Use PRXNEXT to find the first instance of the pattern, */ 
    /* then use DO WHILE to find all further instances.  */ 
    /* PRXNEXT changes the start parameter so that searching */ 
    /* begins again after the last match.      */ 

    call prxnext(ExpressionID, start, stop, text, position, length);  
    do while (position > 0); 
     found = substr(text, position, length); 
     put found= position= length=; 
     call prxnext(ExpressionID, start, stop, text, position, length); 
    end; 
run; 
+2

これはまったく「perl」の質問ではありませんか。私はこれが「魔法の正規表現」の仕事ではなく、むしろ「分割」と同等のものを探していることを示唆しています。 – Sobrique

+1

['。+?(?= \ {2} \ /){2} \ d {4} \ s * \ d {2}(?:: \ d {2}){2 }) '](https://regex101.com/r/Ts05tl/1)? – Gurman

+0

これは正規表現ですが、基本的に文字列の開始/終了を識別する正規表現が必要です。 – TechGuy

答えて

2

ここではそのバリエーションであります「ランドマーク」を探すより単純なパターンに焦点を当てていますテキスト内の。 prxnextループは以前のランドマークを追跡し、それに基づいてfoundを抽出します。

data extracts(keep=line found); 
    if _n_ = 1 then do; 
    rxid = PRXPARSE('/\d{2}\/\d{2}\/\d{4} /'); * nominally mm/dd/yyyy followed by at least one space; 
    retain rxid; 
    end; 

    line = _n_; 

    infile cards _infile_=text; 
    input; 

    start = 1; 
    stop = length(text); 
    position = 0; 
    length = 0; 

    /* Use PRXNEXT to find the first instance of the pattern, */ 
    /* then use DO WHILE to find all further instances.  */ 
    /* PRXNEXT changes the start parameter so that searching */ 
    /* begins again after the last match.      */ 

    call prxnext(rxid, start, stop, text, position, length); 

    lastpos = 0; 
    do while (position > 0); 
    if lastpos then do; 
     length found $200; 
     found = substr(text,lastpos,position-lastpos); 
     put found=; 
     output; 
    end; 
    lastpos = position; 

    call prxnext(rxid, start, stop, text, position, length); 
    end; 

    if lastpos then do; 
    found = substr(text,lastpos); 
    put found=; 
    output; 
    end; 

datalines4; 
01/04/2017 03:45:32 Some Comment - abc 05/04/2017 16:32:41 Some other Comment 06/07 at something 
Some fakery 01/04 /2017 03:45:32 /11/11/1967. yo 01/02/1102 Some Comment - abc 05/04/2017 16:32:41 Some other Comment 06/07 at something 

aabbccdd 
01/02/1903 abc def 
01/02/1903 abc def sas does have trouble matching a patterns having a trailing space 02/03/1904 
;;;; 
run; 

長時間に渡るSASコードを扱う場合、より単純なパターンが適しています。

+0

こんにちは、ありがとうございます:) – TechGuy

関連する問題