492Trying to perfect RegExp to match various numbers
- Mar 28, 2011My ultimate goal was to create a script that (left) space or zero pads numbers to a fixed length for sorting.
Actual data looks something like
File 67817 Id.ppt
File 691037 20dat.sys
File 69870 Lock.doc
File 705 56968.mbs
File 70537 Jil.xls
File 71168 Gas.jpg
I then became interested in trying to find a regexp that will match numbers surrounded by BOL, EOL, spaces and tabs with
signs, decimal point and commas optional.
In the following test data. the numbers starting with 123 should be matched as well as the integers 0, 1 and 2.
xxx456 and 456xx should NOT be matched.
I have something that mostly works but it also matches x456 and the t2 in hmt2. WHY IS THAT?
It also misses 0, 1 and 2 although it does (correctly) pick up -2.
I put in the caret because it was not matching numbers at the start of a line. Is that how it's done?
Thanks for your help. I ordered the Regular Expressions Cookbook today. Hope it's as good as the reviews say!
What I have so far [^\s][\-\+]?\d+,*\d*\.?\d*(?=\s)
there are 0 lines
1 or 2 more.
there are 0 lines. The zero should match as should the following one and negative two.
1 or -2 more.
- Next post in topic >>