2

I'm looking to figure out how to use regular expressions to swap three sections of text separated by quotes/commas. For the purpose of this question I'll call them strings, though I'm not sure if that would be correct. An example is below:

"TEXT_MAP_CENTER","中央地点","Center of Map" 

I need to swap the Japanese text in the second string with the English in the first, so it reads:

"TEXT_MAP_CENTER","Center of Map","中央地点" 

This is just one line of about 4300+ and each string is unique. Thankfully, in this particular file each line is always three strings. There is a second file I need to do this in in which the text wraps.

In order to do this quickly I understand that there are ways to do this using regular expressions, but given that this isn't my normal area of expertise I'm having a real hard time with it. How might I go about doing this?

1 Answer 1

4

Replace

,(".*?"),(".*?")$ 

with

,\2,\1 

The two () capture the two strings, which is referenced by \1 and \2 respectively. Then we replace the first with the second matched string and vice versa

If the strings contain newlines then you just need to select . matches newlines in the regex search option. An alternative way without enabling that option is

Find what: ^("[^"]*"),("[^"]*"),("[^"]*")$ Replace with: \1,\3,\2 
5
  • Thank you so much for this information. This worked flawlessly - I did discover some bits of the file that contained entries that were split across multiple lines (they appear to wrap as if they need to fit a specific size text box). Would there be a similar method for this, or would it be better to edit these manually? Commented Dec 18, 2018 at 23:56
  • I had to manipulate mine, but it works—just not on one of them, for some reason. Thank you so much. The asker should have accepted this as the Answer. Original string URL = Replace(URL,"EM","%19") change to work with your code "URL","EM","%19" The one it does not work with is this one. "URL","""","%22" THANK YOU!!!! Commented Apr 16 at 5:54
  • @WayneBarron It should work: regexr.com/8e4ml. You can also try any online regex builder to debug regextester.com regex101.com Commented Apr 16 at 7:55
  • I'm not sure why, but I could not get it and swap them around. Have you tested it on your end to see if it will swap? It is not a big issue; it is just a little confusing as to why it would not work on that one string out of 49. Commented Apr 16 at 15:20
  • @WayneBarron yes, both "URL","EM","%19" and "URL","""","%22" work as expected in Notepad++'s regex and all regex testers I've tried. I don't know which regex you use, please learn about it and ask a different question in this site Commented Apr 17 at 1:06

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.