regex expression to match decimal numbers with comma as a separator

jakes

As in title I need a regex expression in R that will match decimal numbers with comma as a separator in order to extract them from string with stringr::str_extract . Regex should catch the following cases:

0,1
10
1000,29
3,23313

I have tried something like this [0-9]+,[0-9]+ but it didn't work

Wiktor Stribiżew

You may use

regmatches(x, gregexpr("\\d+(?:,\\d+)?", x))

See this R demo.

To do the same with stringr, use stringr::str_extract_all that "extracts all pieces of a string that match a pattern":

library(stringr)
str_extract_all(x, "\\d+(?:,\\d+)?") 

Note that \d in stringr functions may match all Unicode digits like

0123456789٠١٢٣٤٥٦٧٨٩۰۱۲۳۴۵۶۷۸۹߀߁߂߃߄߅߆߇߈߉०१२३४५६७८९০১২৩৪৫৬৭৮৯੦੧੨੩੪੫੬੭੮੯૦૧૨૩૪૫૬૭૮૯୦୧୨୩୪୫୬୭୮୯௦௧௨௩௪௫௬௭௮௯౦౧౨౩౪౫౬౭౮౯೦೧೨೩೪೫೬೭೮೯൦൧൨൩൪൫൬൭൮൯๐๑๒๓๔๕๖๗๘๙໐໑໒໓໔໕໖໗໘໙༠༡༢༣༤༥༦༧༨༩၀၁၂၃၄၅၆၇၈၉႐႑႒႓႔႕႖႗႘႙០១២៣៤៥៦៧៨៩᠐᠑᠒᠓᠔᠕᠖᠗᠘᠙᥆᥇᥈᥉᥊᥋᥌᥍᥎᥏᧐᧑᧒᧓᧔᧕᧖᧗᧘᧙᭐᭑᭒᭓᭔᭕᭖᭗᭘᭙᮰᮱᮲᮳᮴᮵᮶᮷᮸᮹᱀᱁᱂᱃᱄᱅᱆᱇᱈᱉᱐᱑᱒᱓᱔᱕᱖᱗᱘᱙꘠꘡꘢꘣꘤꘥꘦꘧꘨꘩꣐꣑꣒꣓꣔꣕꣖꣗꣘꣙꤀꤁꤂꤃꤄꤅꤆꤇꤈꤉꩐꩑꩒꩓꩔꩕꩖꩗꩘꩙0123456789

So, probably you will be safer using

str_extract_all(x, "[0-9]+(?:,[0-9]+)?")

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Google form regex for numbers with comma as decimal separator

Parsing numbers with a comma decimal separator in JavaScript

Add comma separator to a numbers with 2 decimal points

Pasting decimal numbers in excel / comma and point decimal separator

Match numbers with decimal separator inside a string

Regular expression to match number with Decimal separator and optional Thousands separator

JavaScript regex to accept numbers with comma separator (thousands) and dot separator (decimals)

Match numbers (decimal and whole) with Regex

Importing xlsx data to R when numbers have a comma as decimal separator

decimal separator in woocommerce to be a comma

Extract numbers, dot and comma using regex match

Regex match numbers and decimals with and without comma

AngularJS: Change decimal separator to comma

Replace point decimal separator with comma

Regex match also negative decimal numbers

Regex for all decimal numbers to match English format

Pandas convert numbers with a comma instead of the point for the decimal separator from objects to numbers

Regex to validate numbers before and after decimal excluding comma

Regex to catch comma-separated decimal numbers not inside latex command

Format a decimal with comma as decimal separator and without thousands separator

Getting numbers behind decimal separator

Regex a decimal number with comma

Making a regex expression for adding single quotes in numbers comma seperated

regex expression extract string after last comma with no numbers

Jquery/Javascript Regular expression to filter decimal numbers with comma, 13 int places and 2 decimal places

Regex match left side of comma-separated expression

Regex for custom decimal and thousand separator

Best way to parseDouble with comma as decimal separator?

Show comma instead of point as decimal separator