簡體   English   中英

用於匹配模式的正則表達式C#搜索文件以和開頭

[英]Regex C# searching file for matching pattern starts with and end with

在C#中,我要搜索匹配發生的位置,該位置以括號中的整數開頭,然后是這些字符“ PLA”括號,然后“匹配”它(我將讀入內存),直到到達下一個集合為止。

因此,示例代碼將是

(1965
    ("PLA")
    ("GEN_ANGLE")
    ("Line to line angle")
    (
        ("clinesegs" 3565.01 1265.99 "SURFACE")
        ("clinesegs" 3618.02 1255.00 "SURFACE")
    )
    ((3586.02 1267.20 "SURFACE"))
    (120.000)
    (90.000)
)
(1966
    ("PLA")
    ("GEN_ANGLE")
    ("Line to line angle")
    (
        ("clinesegs" 3831.98 1255.00 "SURFACE")
        ("clinesegs" 3882.92 1268.07 "SURFACE")
    )
    ((3863.98 1267.20 "SURFACE"))
    (120.000)
    (90.000)
)

我想“匹配”數據並僅在知道“ 1965”是我要查找的ID的基礎上獲取此數據。

(1965
    ("PLA")
    ("GEN_ANGLE")
    ("Line to line angle")
    (
        ("clinesegs" 3565.01 1265.99 "SURFACE")
        ("clinesegs" 3618.02 1255.00 "SURFACE")
    )
    ((3586.02 1267.20 "SURFACE"))
    (120.000)
    (90.000)
)

我可以找到“(1965)”:

(\(1965)  

..或(前面帶有(ADD)):

[(](ADD){1}\r\n\r\n\t\s[(][0-9]{4,}\r\n\r\n\t\s\s(\("){1}[a-zA-Z]{1,}("\)){1}

..但我似乎無法真正使這些類型的正則表達式正常工作,它必須是空格和換行符
我一直堅持了解下PLA的匹配並在下一組數據開始(1966 ("PLA")開始之前“檢測”結尾) ,因為我認為這是我在比賽中用來檢測比賽結束的方法,但不將其包括在調查結果中。

如果您知道在數據內部未出現(1966 ("PLA") ,則可以使用類似以下的內容:

(?xs)                     # ignore spaces, comments and make . match \n
\(1965\s+\("PLA"\)
.*?                       # ungreedy
(?=\(1966\s+\("PLA"\)|$)  # lookahead does not include this in the match

可以在C#中引用如下:

var re = @"(?xs)               # ignore spaces, comments and make . match \n
    \(1965\s+\(""PLA""\)
    .*?                        # ungreedy
    (?=\(1966\s+\(""PLA""\)|$) # lookahead does not include this in the match
    ";

這是我想出的:

編輯 :我修改了表達式以解決N個“線段”的問題,在“線到線角度”組之前和之后的任何數量的組中都可以使用。

\\([\\d]+[\\s]+\\("PLA"\\)([\\s]+\\(.+)+[\\s]+\\(([\\s]+\\(.+)+[\\s]+\\)([\\s]+\\(.+\\))+[\\s]+\\)

這將捕捉一切從(1965直至收盤)

嘗試這種模式:以這種模式在1965年使用您的ID

(?=\(1965)(?=.*\(\"PLA\"\).*)\(((([^)])*\([^)]+\)[^)]*)\))*\)*[^\)]*\)

使用RegexOptions.SingleLine

暫無
暫無

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

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