How to set regexp so that it stops to the first istance?

3 visualizaciones (últimos 30 días)
pietro
pietro el 13 de Jun. de 2017
Editada: Stephen23 el 14 de Jun. de 2017
Hi all,
I need to extract the urls from the following html code and I am using regexp.
a='<option value="http://www.tractordata.com/farm-tractors/003/9/0/3906-massey-ferguson-7465-transmission.html">2004-2007</option><option value="http://www.tractordata.com/farm-tractors/006/7/0/6706-massey-ferguson-7465-transmission.html" selected>2008-2012</option></select></form></td></tr>';
urls=regexp(a,'(?<=option value.*)http.*html','match');
and the result is:
http://www.tractordata.com/farm-tractors/003/9/0/3906-massey-ferguson-7465-transmission.html">2004-2007</option><option value="http://www.tractordata.com/farm-tractors/006/7/0/6706-massey-ferguson-7465-transmission.html
As you can see the sting extract a string which respects the pattern but it includes two different urls. I need the two following results:
http://www.tractordata.com/farm-tractors/003/9/0/3906-massey-ferguson-7465-transmission.html
http://www.tractordata.com/farm-tractors/006/7/0/6706-massey-ferguson-7465-transmission.html
How may I fix this problem?
Thanks
Pietro

Respuesta aceptada

Stephen23
Stephen23 el 14 de Jun. de 2017
Editada: Stephen23 el 14 de Jun. de 2017
You could use a lazy quantifier ? (explained in the regular expression documentation):
>> urls = regexp(a,'(?<=option value.*)http.*?\.html','match');
>> urls{:}
ans =
http://www.tractordata.com/farm-tractors/003/9/0/3906-massey-ferguson-7465-transmission.html
ans =
http://www.tractordata.com/farm-tractors/006/7/0/6706-massey-ferguson-7465-transmission.html
A more robust method would be to not match " characters:
>> urls = regexp(a,'(?<=option value=")[^"]+\.html','match');
If you want to experiment with regular expressions then you might like to try my Interactive Regular Expression Tool, which shows the outputs of regexp as your type the parse and match strings. You can download it here:

Más respuestas (0)

Categorías

Más información sobre String Parsing en Help Center y File Exchange.

Etiquetas

Productos

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by