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:

  1. 2
  2. 2.1
  3. 2.1f or 2.1d
  4. .1f or .1d

What I don't want it to match

  1. 2.
  2. 2.f or 2.d
  3. 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.

Was it helpful?

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
scroll top