Match 1 or more words separated by comma

Viewed 111

I want to match in Emeditor any 1 or more word(s) or phrases (that follow a specific language name) separated by comma and ending in semicolon.

For example, I would like to match this string for Russian:

Russian: радость сть, весе-веселье, весе' веселье, веселье;

And create wiki links in the replacement like this:

Russian: [[радость сть]], [[весе-веселье]], [[весе' веселье]], [[веселье]];

This is the example text:

Bulgarian: веселие, радост; Chinese Mandarin: 歡笑, 欢笑, 愉快, 高興, 高兴; Dutch: vrolijkheid; Finnish: ilo, hilpeys; French: gaieté; Georgian: მხიარულება, სიმხიარულე, სიხარული; German: Fröhlichkeit, Belustigung, Freude; Greek: ευθυμία, κέφι; Ancient Greek: εὐφροσύνη; Italian: gran gioia, allegria; Japanese: 笑い, 遊び, 喜び, 歓楽; Malayalam: ആഹ്ലാദം; Plautdietsch: Freid; Portuguese: alegria, júbilo; Russian: радость сть, весе-веселье, весе' веселье, веселье; Scottish Gaelic: sogan; Spanish: felicidad, alegría, júbilo; Swedish: munterhet, glädje

3 Answers
(?<Lang>Russian): (?<pre>(\[\[.*\]\],)*) (?<words>[^\[,;]+)(?<post>[^;]*?;)

Replace with

$Lang: $pre [[$words]]$post

Run repeatedly.


How's it work? We anchor to text within the word "Russian" and the semicolon. Then we skip past already formatted values by matching them with a repeating capture group. (The anon group within $pre.) Once we find unformatted values (A string that isn't surrounded by [[]], ), we match until the comma/semicolon. Then we match everything after that up to the semicolon.

See it in action with Regex101

Find the Russian line and highlight it. Use 3 (non-regex) replacements with "selected text" option on:

search replace
": " ": [["
", " "]], [["
";" "]];"

Just a couple of lines in the EmEditor macro should do it. 1st line identifies the language and selects that line, 2nd line makes the replacement:

document.selection.Find("(?<=Russian: )[^;]*;",eeFindNext | eeFindReplaceCase | eeFindReplaceRegExp | eeFindMatchDotNL | eeFindSelectAll,0);

document.selection.Replace("(\\b[^,;]+)(?=[,;])","[[\\1]]",eeFindReplaceCase | eeFindReplaceSelOnly | eeReplaceAll | eeFindReplaceRegExp | eeFindMatchDotNL,0);

v2 - Selecting more than 1 language

document.selection.Find("(^|; )((Russian: )|(Greek: )|(French: ))\\K[^;]*;",eeFindNext | eeFindReplaceCase | eeFindReplaceRegExp | eeFindSelectAll,0);

document.selection.Replace("(\\b[^,;]+)(?=[,;])","[[\\1]]",eeFindReplaceCase | eeFindReplaceSelOnly | eeReplaceAll | eeFindReplaceRegExp | eeFindMatchDotNL,0);
Related