Lisp regexp matching numbers with or without constant type identifiers ( 3.2 or 3.2f )
Question
Question: I'm looking to create a number font-lock face in emacs but I'm having trouble getting a regular expression that matches what I need. Here is what I have:
"\\<\\([0-9]*\\.?[0-9]*\\)+\\(d\\|f\\)?\\>"
What I want it to match:
- 2
- 2.1
- 2.1f or 2.1d
- .1f or .1d
What I don't want it to match
- 2.
- 2.f or 2.d
- f or d
Current problems:
Currently it works mostly but it doesn't match numbers like .2 or .2f because it doesn't pick up the (.) before the numbers. It also currently matches a single "f" or "d" anywhere in the code so variable names as f or d get matched. What am I missing here? I've fiddled with this every lunch break for the last week or so, I'm stumped.
Solution
You could use a regex like this (PCRE syntax):
(\d*\.\d+|\d+)[fd]?
(Anchor it as required.)
Translated to Emacs style regex (by Jon O):
"\\<\\([0-9]*\\.[0-9]+\\|[0-9]+\\)[df]?\\>"
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow