第一次出现的正则表达式匹配

Regex match for first occurrence

我想以“[”(方括号)开头。之后我需要找到常量字符串“FIELDS THROWING ERROR =>”(常量字符串),该字符串将出现在字符串中的某些行之后。接下来,我需要在常量字符串之后取一个词(这个词将是动态的)然后我必须在成功匹配模式后停止。

sample string: 

------------------------------------------------
Start Method SYNC DATA :: xxx : 5/19/2022 11:09:28 PM : Total Sync Time : 0.00
----------------------------------------------
[xxx][xxx] Upsert Failed : 
     RECORD NUMBER => ABC:000000
     ERROR MESSAGE => There's a problem with this country, even though it may appear correct. Please select a country/territory from the list of valid countries.: Bilcntry
     FIELDS THROWING ERROR => Bilcntry
[xxx][xxx] Upsert Failed : 
    RECORD NUMBER => ABC:000000
    ERROR MESSAGE => There's a problem with this country, even though it may appear correct. Please select a country/territory from the list of valid countries.: Bilcntry
    FIELDS THROWING ERROR => Bilcntry
[xxx][xxx] Upsert Failed : 
    RECORD NUMBER => ABC:000000
    ERROR MESSAGE => There's a problem with this country, even though it may appear correct. Please select a country/territory from the list of valid countries.: Bilcntry
    FIELDS THROWING ERROR => Bilcntry
[xxx][xxx] Upsert Failed : 
    RECORD NUMBER => ABC:000000
    ERROR MESSAGE => There's a problem with this country, even though it may appear correct. Please select a country/territory from the list of valid countries.: Bilcntry
    FIELDS THROWING ERROR => Bilcntry

Desire Output:

[xxx][xxx] Upsert Failed : 
     RECORD NUMBER => ABC:000000
     ERROR MESSAGE => There's a problem with this country, even though it may appear correct. Please select a country/territory from the list of valid countries.: Bilcntry
     FIELDS THROWING ERROR => Bilcntry

谁能帮帮我?

谢谢

我不知道它是否有助于 SUMO,但在 JAVA 中的实现可能是这样的:

    Pattern pattern = Pattern.compile(
        "^\[[^]]+]\[[^]]+][^\[]+FIELDS THROWING ERROR => (\w+)$", 
        Pattern.MULTILINE | Pattern.DOTALL);
    Matcher matcher = pattern.matcher("------------------------------------------------\n" +
            "Start Method SYNC DATA :: xxx : 5/19/2022 11:09:28 PM : Total Sync Time : 0.00\n" +
            "----------------------------------------------\n" +
            "[xxx][xxx] Upsert Failed : \n" +
            "     RECORD NUMBER => ABC:000000\n" +
            "     ERROR MESSAGE => There's a problem with this country, even though it may appear correct. Please select a country/territory from the list of valid countries.: Bilcntry\n" +
            "     FIELDS THROWING ERROR => Bilcntry\n" +
            "[xxx][xxx] Upsert Failed : \n" +
            "    RECORD NUMBER => ABC:000000\n" +
            "    ERROR MESSAGE => There's a problem with this country, even though it may appear correct. Please select a country/territory from the list of valid countries.: Bilcntry\n" +
            "    FIELDS THROWING ERROR => Bilcntry\n" +
            "[xxx][xxx] Upsert Failed : \n" +
            "    RECORD NUMBER => ABC:000000\n" +
            "    ERROR MESSAGE => There's a problem with this country, even though it may appear correct. Please select a country/territory from the list of valid countries.: Bilcntry\n" +
            "    FIELDS THROWING ERROR => Bilcntry\n" +
            "[xxx][xxx] Upsert Failed : \n" +
            "    RECORD NUMBER => ABC:000000\n" +
            "    ERROR MESSAGE => There's a problem with this country, even though it may appear correct. Please select a country/territory from the list of valid countries.: Bilcntry\n" +
            "    FIELDS THROWING ERROR => Bilcntry\n");
    matcher.find();
    System.out.println("entire match = " + matcher.group(0));
    System.out.println("just the dynamic word = " + matcher.group(1));

请注意,我使用了 MULTILINE 和 DOTALL 标志,这些是以您需要的方式获得整个匹配的关键。为了只提取那个动态词,你需要以某种方式对其进行分组(在 JAVA 正则表达式中它是用括号完成的,也许 SUMO 是相似的) 另外,我假设消息中除了第一条加工线开头的两个地方外,其他任何地方都没有“[”。

输出将是:

entire match = [xxx][xxx] Upsert Failed : 
     RECORD NUMBER => ABC:000000
     ERROR MESSAGE => There's a problem with this country, even though it may appear correct. Please select a country/territory from the list of valid countries.: Bilcntry
     FIELDS THROWING ERROR => Bilcntry
just the dynamic word = Bilcntry

这可能会有所帮助,因为这将匹配给定模式的第一次出现。

\[.*]\[.*] .*: \n(?<message>(.|\n)*?) FIELDS THROWING ERROR => (\w.*)

结果是这样的 screenshot of the result

^\[.*([A-Z\s]*=>[ A-Za-z0-9\:\[\]\-\_\,\.\?\'\/\“\”\"\(\)\;\!\@\#$\%\^\&\*\{\}\|\\+]*)*

匹配几行后的特定单词后,它将起作用。