5 ms·
\s\w+\s ;)
by flir 2y ago
\s\w+\s
;)
- eesmith 2y ago"Ain't ain't a word 'cause it don't match the regex." BTW, should be "\b\w+\b". The \b is a zero-width match for the start or end of a word. Your pattern requires a space before and after: >>> import re >>> re.compile(r"\b\w+\b").findall("What's the problem?") ['What', 's', 'the', 'problem'] >>> re.compile(r"\s\w+\s").findall("What's the problem?") [' the ']
- flir 2y agoThat makes sense. I was trying to translate "A word is a group of letters with whitespace on either side" into regex. I was being facetious, obviously, but you've already identified a serious problem with that definition (is "what's" two words or one?)
- koito17 2y agocljs.user> (re-seq #"\s\w+\s" "やり直して") nil Joking aside, is it common to use regular expressions? Seems like the method only works for languages with spaces. I think a more sophisticated lexer may be necessary, but are there are non-regex, "fast approximations" that work across most languages? This is a problem that I have not tried solving before.
- nine_k 2y agoすみません! It's because you have no space around. A more correct regexp would be \b\w+\b, with zero-width "word boundary" psttetbs instead of spaces.
- Liquid_Fire 2y agoThat's just passing the problem onto how you define \b. Since Japanese uses no spaces, it would match entire phrases or sentences as "words", treating only punctuation as word boundaries.