简体   繁体   中英

Regex: match “:” and “-” but don't match “I-”

Ohare:Montrose:I-290 Circle:IL:IL
Ohare-Montrose-I_290-Circle-IL-IL
EB:Kennedy Expy:O'Hare:IL-43 (Harlem Ave):IL:IL

NB:I-894/US-45:Hale Interchange:Zoo Interchange:WI:IL

NB
I-894/US-45
Hale
Interchange
Zoo Interchange
WI
IL

WB:Indiana-East-West:Eastpoint:Middlebury:IN:25:IL

WB
Indiana-East-West
Eastpoint
Middlebury
IN
25
IL

Trying to extract words from two different sources that use different conventions.

Using regex for that, I cannot create one regex that deals with both options.

If I try to extract using : or - then the first one gets extracted as

Ohare, Montrose, I, 290 Circle, IL, IL

How can I get a regex to split on : or - but ignore I- or ignore 'IL-', 'US-', 'Indiana-East-West' and many other that I may find?

What I have so far but not working as I want Regex

You can use this negative lookbehind regex:

(?:(?:IL?|US)-|Indiana-East-West)(*SKIP)(*F)|[:-]

RegEx Demo

Example Code:

$s = 'NB:I-894/US-45:Hale Interchange:Zoo Interchange:WI:IL';
print_r(preg_split('/(?:(?:IL?|US)-|Indiana-East-West)(*SKIP)(*F)|[:-]/' , $s));
Array
(
    [0] => NB
    [1] => I-894/US-45
    [2] => Hale Interchange
    [3] => Zoo Interchange
    [4] => WI
    [5] => IL
)

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