-1

I have a big .yml translation file, in which I would like to invert the keys and the values.

what I have :

some english sentence : une phrase en francais
another one : une autre

what I would like to get :

une phrase en francais : some english sentence
une autre : another one

I'm sure there might be a bunch of easy regex to do so, but I could not figure out how to do this ...

I dont search a script, just a regex

Anthon
  • 69,918
  • 32
  • 186
  • 246
singe batteur
  • 401
  • 2
  • 14
  • Are all your keys and values so simple? In particular does none of the sentences have a `:` embedded? Like the valid YAML `'He said: "Some English sentence"': 'Il dit: "une phrase en francais"'` – Anthon Aug 04 '16 at 14:27
  • I only had one containing double dots, I changed it manually. thats indeed something to care about – singe batteur Aug 04 '16 at 14:34
  • 1
    So after we all agreed that [parsing XML with regex is a bad idea](http://stackoverflow.com/a/1732454/347964), we now try do parse YAML with regex instead? – flyx Aug 04 '16 at 15:36
  • I think your comment really helps, @flyx ! thanks a lot – singe batteur Aug 06 '16 at 14:19

2 Answers2

2

The regex:

(.*)\s+:\s+(.*)

The substitution:

$2 : $1

Demo: https://regex101.com/r/lU6eM4/2

Dmitry Egorov
  • 9,542
  • 3
  • 22
  • 40
2

you can start with a simple regex :

^(.*):(.*)$

^ = start of line

(.*) = first match group

: = catch :

(.*) = second match group

$ = end of line

Using for exemple notepad++ you can do :

search : ^(.*):(.*)$

replace \2:\1

where \2 :content of second match group and where \1 :content of first match group

This method work only if you sentence don't contain :

baddger964
  • 1,199
  • 9
  • 18