Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RegEx How to handle zero length strings?

New to REgEX and in my sixties so bear with me Using ColdFusion so presumably java version(if there is one)

Looping through some repeated text including picking up values like 4.95 and 4 from

<td align="right" >4.95</td> 

<td align="right" >4</td>

using regex

.+?>(.+?)</td>.+?>(.+?)</td>

but having problems when there is no value i.e.come across string like

<td align="right" ></td>

How would I go about returning a null or 0 in this situation

TIA

like image 416
pssguy Avatar asked Mar 01 '11 17:03

pssguy


1 Answers

Change the + to a * in the relevant places:

...(.*?)...

A .+ matches one or more characters, whereas .* matches zero or more characters. The resulting capture will be an empty string.

Also, I'd advise against using regular expressions to parse HTML. Look to see if there is an HTML parser available in your programming language.

like image 108
Mark Byers Avatar answered Sep 28 '22 17:09

Mark Byers