我堅持創建一個匹配和分組任意數量數字的reg ex,除非它后面直接跟一個字母A
我想要的是:
1 -> 1
1B -> 1
11B -> 11
1A -> nothing
11A -> nothing
在我擁有的 reg ex 中(https://regex101.com/r/jdlDe9/1),我使用了否定的前瞻:.*(\d )(?!A)。如果單個數字后跟一個,它可以作業,A但如果多個數字后跟一個,它仍然匹配A
1 -> 1 OK
1B -> 1 OK
12B -> 12 OK
1A -> nothing OK
12A -> 1 NOT OK. Here it matches the first `1` but I don't want to match anything
uj5u.com熱心網友回復:
在圖案的量詞.*(\d )(?!A)允許回溯,所以在\ d 匹配12的12A可回溯一個位置,并具有超前評估為true。
您可以匹配捕獲 1 個數字,然后匹配除 A 或數字之外的任何字符,或斷言字串的結尾
(\d )(?:[^A\d]|$)
正則運算式演示
或者,如果支持所有格量詞,則在匹配后不允許回溯,并且您只能在沒有捕獲組的情況下獲得匹配:
\d (?!A)
正則運算式演示
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/409273.html
標籤:
