Lisp regexp matching numbers with or without constant type identifiers (3.2 or 3.2f)

Question: I am looking to create multiple font characters in emacs, but I am having trouble getting a regular expression that matches what I need. Here is what I have:

"\\<\\([0-9]*\\.?[0-9]*\\)+\\(d\\|f\\)?\\>" 

I want it to match:

  • 2
  • 2.1
  • 2.1f or 2.1d
  • .1f or .1d

That I do not want it to fit

  • 2.
  • 2.f or 2.d
  • f or d

Current issues:

Currently it works mostly, but it does not match numbers like .2 or .2f because it does not match (.) Before numbers. It also currently matches a single “f” or “d” anywhere in the code, so the variable names match f or d. What am I missing here? I played with this every lunch break last week or so, I'm at a standstill.

+4
source share
1 answer

You can use a regex like this (PCRE syntax):

 (\d*\.\d+|\d+)[fd]? 

(tie it as needed.)

Translated to Emacs-style regular expression ( Jon O ):

 "\\<\\([0-9]*\\.[0-9]+\\|[0-9]+\\)[df]?\\>" 
+2
source

All Articles