简体   繁体   中英

Multiline Regex Matching Issue

I have the following string that I am trying to match via regex:

;IF TEST_DATE <= 200112 THEN E>=90 AND S>=90
 OR P = "25" ENDIF
IF TEST_DATE >= 200201 AND TEST_DATE < 200407 THEN E>=89
AND S>=90 OR P = "25" ENDIF

I am using the following regex in an attempt to match from the semicolon (intended to be a comment) until the first ENDIF:

;\s*IF (\d|\D)+ ENDIF

Unfortunately, this pattern matches all the way until the second ENDIF. I've tried various solutions using the Java Pattern.DOTALL, as well as the (?s) flag, with no luck.

You are using greedy quantifier, due to which your pattern (\\d|\\D) matches everything till it finds the last ENDIF .

You need to use reluctant quantifier - +? if you want your regex to stop matching at the first ENDIF : -

;\s*IF (\d|\D)+? ENDIF

使用非贪婪的限定词。

;\s*IF (\d|\D)*? ENDIF

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM