Regex to replace comma to dot separator
You can use capture groups and reference them in the replacement:
gsub("(\\d+),(\\d+)", "\\1.\\2", "Today I bought apples, oranges for 3,55 dollars")
# [1] "Today I bought apples, oranges for 3.55 dollars"
Replace comma with period in string only if it is a decimal number
Capture a digit, match a comma, lookahead for another digit, and replace with the captured group plus a period:
var string = "This is the number 9,5 but problems is if we have e.g. 10, 11."console.log( string.replace(/(\d),(?=\d)/g, '$1.'));
Replace ,(comma) by .(dot) and .(dot) by ,(comma)
Use replace
with callback function which will replace ,
by .
and .
by ,
. The returned value from the function will be used to replace the matched value.
var mystring = "1,23,45,448.00";
mystring = mystring.replace(/[,.]/g, function (m) { // m is the match found in the string // If `,` is matched return `.`, if `.` matched return `,` return m === ',' ? '.' : ',';});
//ES6mystring = mystring.replace(/[,.]/g, m => (m === ',' ? '.' : ','))
console.log(mystring);document.write(mystring);
How do I replace commas with dot in the quoted sub-string only?
You can do that using preg_replace
$string = preg_replace('/("\d+),(\d+")/','$1.$2',$string);
How to replace comma with a dot in the number (or any replacement)
As replace()
creates/returns a new string rather than modifying the original (tt
), you need to set the variable (tt
) equal to the new string returned from the replace
function.
tt = tt.replace(/,/g, '.')
JSFiddle
Replacing comma's by dots in floats using regular expressions
just replace (\d),(\d)
with \1.\2
don't have notepad++. in eclipse it worked.
Replacing comma by dot only in numbers in a texteditor using search and replace function
EditPlus does not support lookbehinds, so you need to use groups.
(\d)\.(\d)
Substitution: \1,\2
()
Capturing group\d
matches a digit (equal to[0-9]
)\1
\2
Capturing group 1. and group 2.
Related Topics
Splitting a Dataframe into Several Dataframes
Conditional Replacement of a Comma With a Dot in a Numeric Column
Add Row to a Data Frame With Total Sum for Each Column
Filter Data.Frame Rows by a Logical Condition
Pass a Data.Frame Column Name to a Function
How to Use R'S Ellipsis Feature When Writing Your Own Function
Dcast Warning: 'Aggregation Function Missing: Defaulting to Length'
How to Convert Variable With Mixed Date Formats to One Format
What Exactly Is Copy-On-Modify Semantics in R, and Where Is the Canonical Source
Global and Local Variables in R
Multi-Row X-Axis Labels in Ggplot Line Chart
How to Remove the Negative Values from a Data Frame in R
How to Generate a Histogram for Each Column of My Table
Count Number of Rows Within Each Group
Error in If/While (Condition) {: Missing Value Where True/False Needed
Show Percent % Instead of Counts in Charts of Categorical Variables