[fpc-pascal] How to split file of whitespace separated numbers?

Graeme Geldenhuys mailinglists at geldenhuys.co.uk
Tue Dec 27 01:18:00 CET 2016


On 2016-12-24 11:52, Mark Morgan Lloyd wrote:
> regexes are useful.

Very much so, and I’m far from being an expert. But I am finding more
and more uses for them, the more I use them.


> BUT FFS DOCUMENT WHAT YOU'RE DOING FOR PEOPLE WHO DON'T UNDERSTAND THEM!

The same can be said for standard code too. At least with regex, there
are plenty of tools that explain what they do, and what each part means.

For example:

http://rick.measham.id.au/paste/explain.pl?regex=%5Cb%5B-%2B%5D%3F%5B0-9%5D%2B%28%3F%3A%5C.%5B0-9%5D%2B%29%3F%5Cb


NODE                     EXPLANATION
--------------------------------------------------------------------------------
  \b                       the boundary between a word char (\w) and
                           something that is not a word char
--------------------------------------------------------------------------------
  [-+]?                    any character of: '-', '+' (optional
                           (matching the most amount possible))
--------------------------------------------------------------------------------
  [0-9]+                   any character of: '0' to '9' (1 or more
                           times (matching the most amount possible))
--------------------------------------------------------------------------------
  (?:                      group, but do not capture (optional
                           (matching the most amount possible)):
--------------------------------------------------------------------------------
    \.                       '.'
--------------------------------------------------------------------------------
    [0-9]+                   any character of: '0' to '9' (1 or more
                             times (matching the most amount
                             possible))
--------------------------------------------------------------------------------
  )?                       end of grouping
--------------------------------------------------------------------------------
  \b                       the boundary between a word char (\w) and
                           something that is not a word char


That tells you exactly what each part of the following regex means:

   ⌜\b[-+]?[0-9]+(?:\.[0-9]+)?\b⌟

Note:
  I wrap the regex with ⌜ and ⌟ character to denote the start and
  end of the regex. Some people get confused when you use double
  quotes.


I believe Perl or egrep or something can output the exact same “regex
explanation” information from the command line.


Regards,
  Graeme

-- 
fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal
http://fpgui.sourceforge.net/

My public PGP key:  http://tinyurl.com/graeme-pgp



More information about the fpc-pascal mailing list