简体   繁体   中英

Get string between words, and one word is optional

I have a lot of strings like this:

"Dirección: AV. RIVADAVIA 11440 Horario de atención: 08:01 a 21:00 hs"
"Dirección: AV. RIVADAVIA 11441 Horario de atención: 08:02 a 21:00 hs"
"Dirección: AV. RIVADAVIA 11442 Horario de atención: 08:03 a 21:00 hs"
"Dirección: AV. RIVADAVIA 11443 Horario de atención: 08:04 a 21:00 hs"
"Dirección: xxxxx Horario de atención: 08:05 a 21:00 hs"

The first problem: I need to get the text that is where the "xxxxx" are placed. It can be one word and maybe more, its an address.

The second problem: the text after the address is optional, so I can have something like this

"Dirección: CALLE 137 E/ 32 Y 32BIS"

How can I get the address from my strings?

You can use a regex with lookaround assertions and a non-capturing group to match on the end of the line where the optional text after the address is not present; eg:

$strings = <<<EOF
Dirección: AV. RIVADAVIA 11440 Horario de atención: 08:01 a 21:00 hs
Dirección: AV. RIVADAVIA 11441 Horario de atención: 08:02 a 21:00 hs
Dirección: AV. RIVADAVIA 11442 Horario de atención: 08:03 a 21:00 hs
Dirección: AV. RIVADAVIA 11443 Horario de atención: 08:04 a 21:00 hs
Dirección: xxxxx Horario de atención: 08:05 a 21:00 hs
Dirección: CALLE 137 E/ 32 Y 32BIS
EOF;

$regex = '/(?<=Dirección:\s).*(?:(?=Horario de atención)|$)/';
preg_match_all($regex, $strings, $matches);

print_r($matches);

Yields:

Array
(
    [0] => Array
        (
            [0] => AV. RIVADAVIA 11440 
            [1] => AV. RIVADAVIA 11441 
            [2] => AV. RIVADAVIA 11442 
            [3] => AV. RIVADAVIA 11443 
            [4] => xxxxx 
            [5] => CALLE 137 E/ 32 Y 32BIS
        )
)

Hope this helps:)

You can use a tempered greedy token to obtain what you need:

Dirección:\s*((?:(?!Horario de atención:).)*)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

See regex demo , the result is in Group 1:

$re = '~Dirección:\s*((?:(?!Horario de atención:).)*)~u'; 
$str = "Dirección: CALLE 137 E/ 32 Y 32BIS\nDirección: AV. RIVADAVIA 11440 Horario de atención: 08:01 a 21:00 hs"; 
preg_match_all($re, $str, $matches);
print_r($matches[1]);

See demo .

The tempered greedy token (?:(?:Horario de atención.).)* matches anything but a symbol that is starting a substring Horario de atención: .

Based on your example; you just need to look for the zipcode in the address as the limiter. For example

<?php

$strings = <<<EOF
Dirección: AV. RIVADAVIA 11440 Horario de atención: 08:01 a 21:00 hs
Dirección: AV. RIVADAVIA 11441 Horario de atención: 08:02 a 21:00 hs
Dirección: AV. RIVADAVIA 11442 Horario de atención: 08:03 a 21:00 hs
Dirección: AV. RIVADAVIA 11443 Horario de atención: 08:04 a 21:00 hs
Dirección: xxxxx Horario de atención: 08:05 a 21:00 hs
EOF;

preg_match_all('/Dirección:\s(.*\d{5})/', $strings, $matches);

print_r($matches);

Results in item #2 in the array to have the data you need:

Array
(
    [0] => Array
        (
            [0] => Dirección: AV. RIVADAVIA 11440
            [1] => Dirección: AV. RIVADAVIA 11441
            [2] => Dirección: AV. RIVADAVIA 11442
            [3] => Dirección: AV. RIVADAVIA 11443
        )

    [1] => Array
        (
            [0] => AV. RIVADAVIA 11440
            [1] => AV. RIVADAVIA 11441
            [2] => AV. RIVADAVIA 11442
            [3] => AV. RIVADAVIA 11443
        )

)

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