简体   繁体   中英

Find and Replace with Regex in Notepad++ Using Wildcards

I have the following two lines as an example, that's part of a much larger data set. The file is quote text qualified with a vertical bar column delimiter. You have examples of too many quotes that are blowing up my import.

"BD 3 ML SYRINGE 18GX1-1/2""|"0"|""|"10"|"MISCELLANEOUS"

For example: 18GX1-1/2""| SHOULD BE: 18GX1-1/2"|

"BD 3 ML SYRINGE 25GX1""|"0"|""|"10"|"MISCELLANEOUS"

For example: 25GX1""| SHOULD BE: 25GX1"|

I can easily find all instances of the error in my file using regex with [\\w]""|

But I cannot find the right expression to simply replace the duplicate double quote with a single dbl quote replacing all words found with the original expression.

Thanks in advance for any advice!

I don't think you need a regular expression to do this. Why don't you just type "" into the find and " into the replace . I tried it on your example input:

"BD 3 ML SYRINGE 18GX1-1/2""|"0"|""|"10"|"MISCELLANEOUS"

在此处输入图片说明

Output:

"BD 3 ML SYRINGE 18GX1-1/2"|"0"|"|"10"|"MISCELLANEOUS"

I would try find "+([^"|]+)"+ , replace with "\\1" :

Capture any sequence of characters other than " and | , where the sequence is surrounded by any number of " ; replacing by the captured group in a single pair of quotes.

In general, the pattern delimiter(non-delimiters)delimiter is quite useful for delimited text searches.

For your test inputs

String 1: "BD 3 ML SYRINGE 18GX1-1/2""|"0"|""|"10"|"MISCELLANEOUS"
becomes:  "BD 3 ML SYRINGE 18GX1-1/2"|"0"|""|"10"|"MISCELLANEOUS"

String 2: "BD 3 ML SYRINGE 25GX1""|"0"|""|"10"|"MISCELLANEOUS"
becomes:  "BD 3 ML SYRINGE 25GX1"|"0"|""|"10"|"MISCELLANEOUS"

Edit: handling delimiter inside quoted string and escaped double quote

Although not mentioned in the question, I thought it may be good to also handle the delimiter and possibly also escaped double quote characters inside the quoted string

The search pattern (^|\\|)"+([^"]*)"+(?=\\||$) and replace pattern \\1"\\2" builds on the above, but permits | inside a quoted string

String 3: "BD 3 ML SYRINGE 25GX1""|"0"|""|"10"|""MISCELL|ANEOUS""
becomes:  "BD 3 ML SYRINGE 25GX1"|"0"|""|"10"|"MISCELL|ANEOUS"

The search pattern (^|\\|)"+(([^"\\\\]|\\\\.)*)"+(?=\\||$) and replace pattern \\1"\\2" further permits escaped double quotes \\" inside the quoted string

String 4: "6\" Tube""|"0"|""|"10"|""MISCELL|ANEOUS""
becomes:  "6\" Tube"|"0"|""|"10"|"MISCELL|ANEOUS"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM