我有這樣的字串:
WILLIAM SMITH 2345 GLENDALE DR RM 245 ATLANTA GA 30328-3474
LINDSAY SCARPITTA 655 W GRACE ST APT 418 CHICAGO IL 60613-4046
我想確保我得到的字串就像上面的那些字串。
這是我的正則運算式:
[A-Z] [A-Z] [0-9]{3,4} [A-Z] [A-Z]{2,4} [A-Z]{2,4} [0-9] [A-Z] [A-Z]{2} [0-9]{5}-[0-9]{4}$
但是我的正則運算式只匹配第一個示例而與第二個不匹配。
uj5u.com熱心網友回復:
嘗試這個:
^[A-Z] [ \t] [A-Z] [ \t] \d .*[ \t] [A-Z]{2}[ \t] \d{5}(?:-\d{4})$
演示
解釋:
1. ^[A-Z] [ \t] [A-Z] [ \t] Starting at the start of line,
two blocks of A-Z for the name
(however, names are often more complicated...)
2. \d .*[ \t] [A-Z]{2}[ \t] Using number start and
two letter state code at the end for the full address
Cities can have spaces such as 'Miami Beach'
3. \d{5}(?:-\d{4})$ Zip code with optional -NNNN with end anchor
uj5u.com熱心網友回復:
這是帶有捕獲組的 dawg 正則運算式:
^([A-Z] [ \t] [A-Z] )[ \t] (\d )[ \t](.*)[ \t] ([A-Z]{2})[ \t] (\d{5}(?:-\d{4}))$
這是網址。
更新
抱歉,我忘了在 dawg 的正則運算式末尾洗掉非捕獲組......
這是沒有非捕獲組的新正則運算式:regex101
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/334550.html
