pssguy
pssguy

Reputation: 3515

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

Upvotes: 5

Views: 2329

Answers (1)

Mark Byers
Mark Byers

Reputation: 838376

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.

Upvotes: 9

Related Questions