How to test if a gawk string contain a number?
- by Tim Menzies
In gawk I know two ways to test if a string contains a number. Which is best?
Method one: using regular expressions:
function method1(x) {
return x ~ /^[+-]?([0-9]+[.]?[0-9]*|[.][0-9]+)([eE][+-]?[0-9]+)?$/
}
Method two: the coercion trick (simpler):
function method2(x) {
return (x != "") && (x+0 == x)
}
Is there any reason to favor the more complex method1 over the simpler method2?