среда, 30 декабря 2009 г.

This one's tricky

This one's tricky

I'm not an expert by any stretch of the imagination with regular expressions, but I always like to jump at the opportunity to expand my knowledge of them. I like to use The Regex Coach to test out any expressions that I may need to create, and I was able to create something that would work in a language other than JavaScript. What I ended up with is:

[0-9,]+[.]+[0-9]{2}

If the string in question is 23,192.168.531.110, the above expression would return 23,192.16, but like I noted, this won't work in JS. I did some searching for floating number regular expressions, but none of the many I tried were able to work properly; still, this shouldn't discourage you from searching and tinkering yourself. A second way to do it is to split the string up by the decimal and then join the first and second items together after applying, but this isn't ideal; another way would be to parseFloat() on the string, but again, this is not ideal. I'll see if there's a way that I can have this work properly, though the point of this script is to split fields up by characters such as a decimal point, so you'd end up with something like:

.

Комментариев нет:

Отправить комментарий