Regular Expressions 101

Community Patterns

Community Library Entry

0

Regular Expression
PCRE (PHP <7.3)

/
^(\d+\.)?(\d{3})+(?=\S+)
/
gm

Description

%# 1. create your formated string x = 12345678; str = sprintf('%d',x)

%# 2. use regexprep to add commas %# flip the string to start counting from the back %# and make use of the fact that Matlab regexp don't overlap %# The three parts of the regex are %# (\d+.)? - looks for any number of digits followed by a dot %# before starting the match (or nothing at all) %# (\d{3}) - a packet of three digits that we want to match %# (?=\S+) - requires that theres at least one non-whitespace character %# after the match to avoid results like ",123.00"

str = fliplr(regexprep(fliplr(str), '(\d+.)?(\d{3})(?=\S+)', '$1$2,'))

Submitted by anonymous - 7 years ago