I wrote an open source project in PHP and as an exercise I’m creating an Objective-C port to run as a desktop app in OSX.
I’m stumped trying to convert the following regular expression from PHP to Objective-C. The problem is that I don’t even know what class to even use. I’ve attempted to use NSRange and NSRegularExpression without much success.
Here’s my PHP code:
$pattern = '/' .
'([\p{Han}]+)' . // One or more kanji
'(([\p{Hiragana}]*))' . // Hiragana between japanese style parentheses
'/u';
return preg_filter($pattern, '<ruby><rb>$1</rb><rp>(</rp><rt>$2</rt><rp>)</rp></ruby>', $this->_text);
This should turn this:
林(はやし)さんは英語(えいご)は話(はな)せます。
into this:
<ruby><rb>林</rb><rp>(</rp><rt>はやし</rt><rp>)</rp></ruby>さんは<ruby><rb>英語</rb><rp>(</rp><rt>えいご</rt><rp>)</rp></ruby>は<ruby><rb>話</rb><rp>(</rp><rt>はな</rt><rp>)</rp></ruby>せます。
EDIT:
I’ve tried some string replacing examples in the Apple Docs but the part that I can never get right is the actual regex pattern. I don’t know how to represent the \p{Han} and \p{Hiragana} subsets in Objective-C.
NSError *error = NULL;
NSRegularExpression *regex =
[NSRegularExpression regularExpressionWithPattern:@"some pattern here"
options:NSRegularExpressionCaseInsensitive
error:&error];
This is usually when I kick myself in the head…
As it turns out, Objective-C DOES accept
\p{Han}, except that I had to escape the backslash otherwise it wasn’t recognizing it!