2

I want to replace all text like this:

"latitude": "32.336533",

with this:

"latitude": 32.336533,

I'm using Notepad++.

7

Using Regex use the following pattern:

"([0-9]+\.{0,1}[0-9]*)"

and Replace with:

\1

This worked for me with replace all function of notepad++. It would find "12." too and remove the double quotes. For a more comprehensive search use this Regex pattern:

"(\-{0,1}[0-9]+(\.[0-9]+){0,1})"

which actually will find negative numbers as well and matches floats only with digits after the decimal point.

Explanation:

it will match

    "             ; a leading double quote
    (             ; followed by the outer subpattern (in backreference \1:
      \-{0,1}       ; an optional minus sign
      [0-9]+        ; followed by 1 or more decimal digits (could be replaced by \d)
      (             ; followed by the next subpattern
         \.           ; a decimal point
         [0-9]+       ; followed by 1 or more digits
      ){0,1}        ; maximal 1 occurrence of this subpattern, and it's optional
    )             ; end of the outer subpattern
    "             ; followed by the trailing double quote

Backreference \1 includes everything in the outer subpattern including the inner one, if it exists. You could use \d for the [0-9] classes and use the question mark ? instead of the last {0,1} group. Remember that the use of the ? may change the greediness of patterns.


Example:

the text in notepad++ with following lines

"latitude": "-32.336533",
"latitude": "32.336533",
"foo": "14"
"bla": "12."
"to7": "12.a"

will be changed after applying "Replace all" to

"latitude": -32.336533,
"latitude": 32.336533,
"foo": 14
"bla": "12."
"to7": "12.a"
3
  • This assumes that each number is a floating point number. However, this is the more correct answer thus far. May 13 '14 at 22:16
  • @KronoS The original did match integers too. I've edited the regexp to include negative numbers and demanding at least one digit after the decimal point (last group optional).
    – VMai
    May 13 '14 at 22:53
  • Your updated answer works great and is a great solution to the problem handling cases where the user probably wants string types to stay string and actual numbers to be converted to numbers. May 14 '14 at 15:26
0

Regex match

"([\d\.]+)"

Replace with

\1
1
  • This doesn't handle negative numbers, and will match unwanted strings like "...", "3..2", "2014.05.13", or "127.0.0.1".
    – and31415
    May 13 '14 at 23:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.