Regex not operator

前端 未结 3 570
刺人心
刺人心 2020-11-28 22:13

Is there an NOT operator in Regexes? Like in that string : \"(2001) (asdf) (dasd1123_asd 21.01.2011 zqge)(dzqge) name (20019)\"

I want to delete all

3条回答
  •  渐次进展
    2020-11-28 22:47

    You could capture the (2001) part and replace the rest with nothing.

    public static string extractYearString(string input) {
        return input.replaceAll(".*\(([0-9]{4})\).*", "$1");
    }
    
    var subject = "(2001) (asdf) (dasd1123_asd 21.01.2011 zqge)(dzqge) name (20019)";
    var result = extractYearString(subject);
    System.out.println(result); // <-- "2001"
    

    .*\(([0-9]{4})\).* means

    • .* match anything
    • \( match a ( character
    • ( begin capture
    • [0-9]{4} any single digit four times
    • ) end capture
    • \) match a ) character
    • .* anything (rest of string)

提交回复
热议问题