![]() |
RegEx & Unicode
I've been using the following regex to abbreviate series names as initialisms:
Code:
\s*([a-zA-Z]|\d+\.?\d*)[a-z\']*\.?\s*Or is my best bet just to manually transcode my series? (yuck) |
Supply a sample(s) and expected result(s), make life easy.
|
Also where are you using this and why?
|
Quote:
Fb Though that's pretty irrelevant. I'm not looking for debugging this particular regex, or to start adding tons of individual unicode characters to it. I'm wondering if calibre's flavor of regex is/can be unicode aware, since I suspect some flavors of regex are, but I've never had occasion to explore the issue before. Alternatively I thought there might be some calibre template functions that would transliterate a unicode string (though that would have other side effects). Quote:
But again, it's more of a general question, since at various times, for various reasons, authors, titles, series, etc., get plugged into regexps, and they all have the occasional unicode character which doesn't fall into the standard [a-zA-Z] or \w range. |
|
Quote:
So I think I'm most of the way there ... but I could still use a little help:) I should be able to replace [a-zA-Z] with (?u)[/w/D] (if I ignore _ for now), right? [edit: of course this doesn't work -- I'm always trying to stick exclusions in a group and it's never worked yet:smack:] But is there an easy equivalent to [a-z] ? |
If you mean only lowercase letters, then no. Though you can use unicode character ranges, like this [\u0028-\u0046] if you know the character ranges you want.
|
Well, I'm stumped again.
I mean -- [\u0000-\uFFFF]* should match anything, including punctuation and two-part characters, right? But not only does it not grab accented characters, it doesn't grab v,w,x,y, or z. :blink: |
I think re.UNICODE only causes \w & \W to match non-ascii characters, at least practically speaking. Which would be okay except that \w also includes numbers - if you're okay with matching numbers then \w+ should be ok.
I've always wished it would make [a-zA-Z] work the way capnm wants. I suppose you might be able to mix it with an digit exclusion lookahead: (?u)(?=[^\d]+)(\w+) But it's going to get tricky. |
Do a bit of googling on how to use unicode char ranges in python regexps. I haven't ever used them myself, so I cannot comment.
|
You can most likely use something like :
Code:
(?i)(?:^|\s+)(\d+\.?\d*?|[\D])Code:
string = r'Föô bár šjohka' |
Quote:
I played/poked around a bit and here's what I found (which may even be correct): This flavor of python regex supports (?u), which makes \w, \d, \b unicode aware. It doesn't support \unnnn or \Unnnnnnnn. It doesn't support upper/lower properties or character classes. :( Revising your lookahead idea, I think this will emulate a unicode aware [a-zA-Z] (?u)\w(?!(?<=[\d_])) but that doesn't solve my wish ... Oh, well. This was supposed to be a quick exercise in tweaking some template code. Now I'm just being stubborn :) Since I don't forsee any great inspiration on how to make a unicode [a-z], I'll probably settle for adding [à-ÿ] to at least make it Latin-1 aware ... |
Quote:
Code:
\s*(\d+\.?\d*\w?|\w)[a-z_\']*\.?\s*is there a functional difference? Thanks! |
Quote:
By specifying that the starting point either has to be the start of a string (careful of multiline issues), this situation is removed as the word can only be separated by one or more spaces. If you want to use it for replacement - as you wanted, the pattern would be : Code:
find: (?iu)(?:^|\s+)((?:\d+\.?\d*?)|(?:[\D]))[\w]+ |
@Serpentine,
Yes -- that makes sense ... and might be a good way to address what led to my latest round of tweaking -- accented chars from the middle of a word popping up in my abbreviations. Of course it will complicate the other tweaking I've done over time to make the abbreviations more readable/pertinent, like including most punctuation, but not periods and quotes, and including numeric strings and all capital letters, and ... :chinscratch: Hmmm ... if I abandon including all capital letters, the rest will probably fall together -- that's probably the unicode sticking point ... After several tweaks, these regexps are probably best rewritten from scratch as they've accumulated redundancies and idiosyncrasies, but sometimes I'm lazy:) Maybe I'll focus on redoing my {author_sort}{series}-->{author} plugboard template for the Sony, since someone else might find it useful ... Thanks! |
| All times are GMT -4. The time now is 10:43 PM. |
Powered by: vBulletin
Copyright ©2000 - 3.8.5, Jelsoft Enterprises Ltd.
MobileRead.com is a privately owned, operated and funded community.