Regular Expressions 101

Community Patterns

Community Library Entry

0

Regular Expression
PCRE (PHP <7.3)

/
(?<=[ \t])-? (?: (?: \d |[1-9]\d+ |[1-9]\d*\.\d{1,3}(?<!0) |\.\d{1,3}(?<!0) )|(?: (\.0*)0 |(0+)\.\d{0,2}[1-9] |(0+)(?:0|[1-9]\d*)\.?\d* |\d+(\.0*) |\d*\.(?:[1-9]|\d[1-9])?(0+) |\d*\.\d{3}(\d+) ) ) (?=[ \t]|$)[ \t]?
/
xgm

Description

Regex to match the valid and preferred number formats inside a LDraw model file and capture invalid parts in the formatting.

Syntax rules:

  • White spacing characters must be spaces, tabs or a mix of both.
  • These numbers must be separated by one or more white spacing characters.
  • For markup, these numbers may have zero or more leading and/or trailing white spacing characters.

Number format rules:

  • must be positive or negative integers or floats (123, 20, 0, 1.2, 0.05)
  • may have a maximum precision of 3 (.1, .01, .001)
  • must have no unnecessary leading or trailing zeros (e.g. 00001 or .200)
  • must have no unnecessary float notation (e.g. 10.)

See reference: LDraw File Format Specification

What it does and what it's used for This regex matches every integer and float (excluding "e" notations). All invalid and/or unpreferred formats will be captured in groups 1 through 6. These groups will contain the invalid parts of a number. Some might have multiple format errors, but only one will be captured. See comments in Test string for which group captures what error. Group 0 contains the entire number including any possible invalid parts.

This regex was created for us as an syntax highlighter for LDraw files in Sublime Text 3.

License This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

Submitted by vHeemstra - 4 years ago