If the format is always this presented in the question, "neighborhood - city - state", and the state is represented by two capital letters, it is even quiet:
>.* - (.*) - ([A-Z]{2})<
(Follow example of regex101 expression: https://regex101.com/r/EPTpOM/1)
That is, a tag lock >
, followed by any string (neighborhood), followed by the separator " - ", followed by any string we want to store in the rematch (city), other separator, other rematch for a pair of uppercase letters (status) and, finally, a tag opening <
.
In the case of PHP you can pass an array to the function preg_match()
. Thus, the city and states will be returned in elements 1 and 2 of the array, respectively:
<?php
$html='<div class="endereco-item">
<h2 class="azulclaro identify">Casa</h2>
<div class="entrelinha_0"></div>
<div class="font_15"></div>
<div class="font_15"></div>
<div class="font_15">R: Antonio Pires dos Santos, 647 praça central</div>
<div class="font_15">Parque santo antonio - Sao Paulo - SP</div>
<div class="font_15">CEP: 55555-555</div>
<div class="font_15">Fone: (11)943-056-295 (55)555-555-555</div>
<div id="ctl00_Body_rptEnderecos_ctl00_dvRadio" class="font_15 custom-checkbox">
<input type="radio" id="radio0" name="radioSelect" checked onclick="setPrincipal(0)" />';
$cidade_estado = array();
$regex = '/>.* - (.*) - ([A-Z]{2})</';
preg_match($regex, $html, $cidade_estado);
print_r($cidade_estado);
(The following is an example of PHP code in repl.it: https://repl.it/NvhF/0)
I believe you are wanting to solve a problem with a tool not suited to this problem. Why did you think of regular expressions?
– Jefferson Quesado
It is that I have tried in other ways and I have not succeeded, I need to put this text in variables to create an array.
– Estudante PHP
The recommended is the use of a DOM Parsing for web Scrapping, then if necessary Regex to make fine adjustments. But try this expression:
[\s\w]+?-\s*[A-Z]{2}(?=<\/div>)
and the demo on Regex101.– danieltakeshi