notepad++ - Remove quotation marks around a number (while leaving other quotes)

08
2014-07
  • user322634

    I want to replace all text like this:

    "latitude": "32.336533",
    

    with this:

    "latitude": 32.336533,
    

    I'm using Notepad++.

  • Answers
  • KronoS

    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"
    
  • Excellll

    Regex match

    "([\d\.]+)"
    

    Replace with

    \1
    

  • Related Question

    Notepad++ regex Replace, using match variables
  • JellicleCat

    In Notepad++, I want to perform a find-and-replace, providing a variable in the 'Replace with' field, which references a match from the 'Find what' field. Any ideas?

    I tried supplying (find) foo (\w) bar (replace) foo baz ($1) bar qux. That didn't work.


  • Related Answers
  • Synetech

    Notepad++ uses standard POSIX regular expressions. As such, backreferences should use the slash instead of the dollar-sign:

    Find    : foo (\w) bar
    Replace : foo baz (\1) bar qux