Search code examples
javascriptregexchinese-locale

Regex for Matching numeral Pinyin


I'm looking for a regex to match numeral pinyin lexical unit (one or more pinyin without space).

Reading Regex for Matching Pinyin seems a good start as I was able to quickly add the support for numeral by doing :

/(ORIGINAL_REGEXP)[0-5]/

So essentially wrapping the old regexp in a group and appending the numeral condition. However I'm not able to extend this to the case of multiple words. For instance :

jiao4zuo4zhi1wu4    叫座之物
jiao4zu3    教祖
jiao4zong1xuan3ju3  教宗选举
jiao4zi3    教子
jiao4zhun3yi2qi4    校准仪器
jiao4zhun3tiao2     校准条
jiao4zhun3ti1chi3   校准梯尺
jiao4zhun3quan1     校准圈
jiao4zhun3qi4   校准器
jiao4zhun3pu3   校准谱 

N.B.: This expression will be used in a Javascript context.


Solution

  • Here is the regexp I'm using based on @EagleV_Attnam solution and some addition what I've done fin:

    /^((ORIGINAL_REGEXP)[0-5])+$/
    

    The addition of the start ^ and end $ anchor solve my issues :)

    Full regex is:

    /^((([mM]iu|[pmPM]ou|[bpmBPM](o|e(i|ng?)?|a(ng?|i|o)?|i(e|ng?|a[no])?|u))|([fF](ou?|[ae](ng?|i)?|u))|([dD](e(i|ng?)|i(a[on]?|u))|[dtDT](a(i|ng?|o)?|e(i|ng)?|i(a[on]?|e|ng|u)?|o(ng?|u)|u(o|i|an?|n)?))|([nN]eng?|[lnLN](a(i|ng?|o)?|e(i|ng)?|i(ang|a[on]?|e|ng?|u)?|o(ng?|u)|u(o|i|an?|n)?|ve?))|([ghkGHK](a(i|ng?|o)?|e(i|ng?)?|o(u|ng)|u(a(i|ng?)?|i|n|o)?))|([zZ]h?ei|[czCZ]h?(e(ng?)?|o(ng?|u)?|ao|u?a(i|ng?)?|u?(o|i|n)?))|([sS]ong|[sS]hua(i|ng?)?|[sS]hei|[sS][h]?(a(i|ng?|o)?|en?g?|ou|u(a?n|o|i)?|i))|([rR]([ae]ng?|i|e|ao|ou|ong|u[oin]|ua?n?))|([jqxJQX](i(a(o|ng?)?|[eu]|ong|ng?)?|u(e|a?n)?))|(([aA](i|o|ng?)?|[oO]u?|[eE](i|ng?|r)?))|([wW](a(i|ng?)?|o|e(i|ng?)?|u))|[yY](a(o|ng?)?|e|in?g?|o(u|ng)?|u(e|a?n)?))[0-5])+$/