Tapatalk

How to remove double quotes at beginning and end of a string using a regular expression replace?

How to remove double quotes at beginning and end of a string using a regular expression replace?

3
NewbieNewbie
3

    Aug 01, 2019#1

    I have a text file and on some of the rows I have a column where the string begins with a double quote (") and ends with a double quote ("). I need to be able to remove the beginning double quote, which always starts in the same position (column # 6), and the ending double quote, which will be in a different position, depending on the length of the string. Also within the string (inside the outer double quotes) I may have other double quotes that need to be preserved and not modified. I'm not sure if this is something that can be accomplished using expressions, and if so what are the necessary search and replace strings.

    Thanks in advance.

    19176
    MasterMaster
    19176

      Aug 02, 2019#2

      Hi,

      another double quotes beyond the ending one are allowed? Are the inner double quotes prefixed by some special character (backslash,...)?

      It both answers are "NOT" then you can try this Perl regular expression:

      F: ^(.{5})"(.*)"
      R: \1\2

      BR, Fleggy

      6,685587
      Grand MasterGrand Master
      6,685587

        Aug 02, 2019#3

        I explain the Perl regular expression posted by Fleggy.

        ^ ... start each search at beginning of a line.

        (...) ... first marking group. The string found by the expression inside this marking group is back-referenced with \1 in replace string.

        .{5} ... any character except newline characters exactly five times.

        " ... the sixth character on a line must be a double quote.

        (...) ... second marking group. The string found by the expression inside this marking group is back-referenced with \2 in replace string.

        .* ... any character except newline characters 0 or more times greedy which means matching characters does not stop on next occurrence of a double quote, but on last occurrence of a double quote on a line.

        " ... matches the last double quote on a line which has as sixth character a double quote.

        A line with sixth character being a double quote, but having no more double quote character is ignored by this search expression.
        Best regards from an UC/UE/UES for Windows user from Austria

        3
        NewbieNewbie
        3

          Aug 02, 2019#4

          Hi Fleggy - I tried that but it did not seem to work.Here are a couple examples that help illustrate my data and requirements:

          Code: Select all

          64644 "REAG TOX VALP ACD FOR VITROS 4600 5600 5,1 FUS SYS"
          64692 "CUP IMMUN ANALZR MIC SAMP DISP FOR VITROS 350 4600 5600 5,1 "
          39831 "BELT RIB M 2PNL 6"" EA"
          63966 "CHAIR TRNSPRT LTWT 19" EA"
          On all of these lines I want to remove the first and last double quote characters, as you can see the ending double quote is in a different position, depending on how long the string is for that line. In the 3rd and 4th lines you see there are double quote characters inside the outer quotes (the ones I want removed), these inner ones I want preserved. Here is what the expected output would look like for these 4 lines:

          Code: Select all

          64644 REAG TOX VALP ACD FOR VITROS 4600 5600 5,1 FUS SYS
          64692 CUP IMMUN ANALZR MIC SAMP DISP FOR VITROS 350 4600 5600 5,1 
          39831 BELT RIB M 2PNL 6"" EA
          63966 CHAIR TRNSPRT LTWT 19" EA

          19176
          MasterMaster
          19176

            Aug 02, 2019#5

            Well, the first double quote is not in column #6 but in #7. Simply change the find regex to:
            ^(.{6})"(.*)"
            The rest remains the same.

            3
            NewbieNewbie
            3

              Aug 02, 2019#6

              Awesome this worked great! Thanks!