Greetings!

I am writing an interface for a search engine that will pull its results from a database. I have, more or less, mastered the entire code in English, and am now trying to remaster it in CJK-compatible fonts. I would prefer to use uft8, and ideally the solution should be compatible with standard Latin characters as well. I am using Perl 5.8.7.

At the core of the problem is that I am unable to get a pattern match for words in a unicode-containing string. Even if I place spaces between the words, the regex will not recognize the space. I have tried \s \p{IsSpace} \P{IsWord} \b \X \p{IsZ} \p{IsZc} and others, with or without "use Encode;" or "use utf8;" in various forms, all to no avail. Here is an example of one line of my code:

1 while $line =~ s/(\p{IsWord})(?<!\p{IsSpace}AND|\p{IsSpace}XOR|\p{ +IsSpace}NOT|\p{IsWord}\p{IsSpace}OR)(\p{IsSpace}|\s|\p{IsMc}|\p{IsZs} +|\p{IsZ})(?!AND\p{IsSpace}|XOR\p{IsSpace}|NOT\p{IsSpace}|\&\&|\&\p{Is +Space}|\+|\|\||\||OR\p{IsSpace}|\^|\!)(\p{IsWord})/$1 AND $3/gi;
Now, that line will function perfectly on an English input string, but as soon as I enter Chinese, it won't match anything. All that line needs to do is to insert an " AND " between any two spaced words, after verifying that those words are not search operator terms themselves. So, XXX YYY OR ZZZ should become XXX AND YYY OR ZZZ.

Any ideas for why this is not performing correctly in utf8? (I'm open to a total rewrite of the line, as it's obvious I haven't found the best solution yet.)

Thank you!


In reply to Unicode substitution regex conundrum by Polyglot

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.