Regular Expression - removing a line(English) and attaching it to the end of upper line(Korean)

Viewed 173

I have this text like below:

아니다
bukan

싫다
tidak suka

훌륭하다
bagus

And I am trying to remove the English line(English Alphabets) and attach it to the end of upper line(Korean Alphabets) like this:

아니다bukan

싫다tidak suka

훌륭하다bagus

Now, Finally find almost close regular expression, which is this:

[가-힣]\R

However, It makes the text file like this:

아니bukan

싫tidak suka

훌륭하bagus

The problem is removing the one word of Korean too.

How can I solve this problem?

2 Answers

C++ std::regex does not support Unicode property classes like \p{Hangul}, but you may use the equivalent character class, [\u1100-\u11FF\u302E\u302F\u3131-\u318E\u3200-\u321E\u3260-\u327E\uA960-\uA97C\uAC00-\uD7A3\uD7B0-\uD7C6\uD7CB-\uD7FB\uFFA0-\uFFBE\uFFC2-\uFFC7\uFFCA-\uFFCF\uFFD2-\uFFD7\uFFDA-\uFFDC], see this reference.

Besides, \R is not supported either. You may probably just use \r?\n to match Windows/Linux style line endings, or (?:\r\n?|\n) to also support MacOS line endings.

Next, if you match and consume a Korean char, when replacing, you need to capture it into a capturing group and use a backreference to the group in the replacement pattern.

So, you may use

([\u1100-\u11FF\u302E\u302F\u3131-\u318E\u3200-\u321E\u3260-\u327E\uA960-\uA97C\uAC00-\uD7A3\uD7B0-\uD7C6\uD7CB-\uD7FB\uFFA0-\uFFBE\uFFC2-\uFFC7\uFFCA-\uFFCF\uFFD2-\uFFD7\uFFDA-\uFFDC])(?:\r\n?|\n)

Replace with $1 to put back the Korean char into the resulting string.

See the regex demo online.

The regex for the set of all Korean characters in unicode is this:

\p{Hangul}

There is more information here: https://www.regular-expressions.info/unicode.html

Maybe you also need a + after your group of characters?

Use the [\p{Hangul}]+\R regular expression instead of what you're using now.

Related