BlackBerry 5 上的正则表达式 PatternRepository 模式 - 如何忽略大小写
我希望这个标题有意义 - 我需要在 BlackBerry 5 上进行不区分大小写的正则表达式匹配。
我有一个正则表达式定义为:
public static final String SMS_REG_EXP = "(?i)[(htp:/w\\.)]*cobiinteractive\\.com/[\\w|\\%]+";
它旨在匹配“cobiinteractive.com/”后跟一些文本。前面的 (htp:w.) 就在那里,因为在我的设备上,我需要覆盖手机应用的内部链接识别(无耻的黑客)。
该应用程序在启动时加载。我的想法是,我想从短信和短信中获取指向我网站的链接。电子邮件,并使用我的应用程序处理它们。
我使用以下方法将其添加到 PatternRepository:
PatternRepository.addPattern(
ApplicationDescriptor.currentApplicationDescriptor(),
GlobalConstants.SMS_REG_EXP,
PatternRepository.PATTERN_TYPE_REGULAR_EXPRESSION,
applicationMenu);
- 在 os 4.5 / 4.7 模拟器上 Curve 8900 设备(运行 4.5), 这有效。
- 在 os 5 模拟器和 Bold 上 9700 我测试过,应用程序无法编译 的图案与 IllegalArgumentException(“无法识别 (?") 之后的字符。
我还尝试(天真地)将模式设置为“/rockstar/i”,但这仅匹配确切的字符串 - 这可能是正确的方向,但如果是这样,我不知道如何在 BB 上实现它。
如何修改我的正则表达式以便使用上面的 PatternRepository 来获取不区分大小写的模式
PS:“正确”的方法是使用 [Cc][Oo 吗? ][Bb][Ii]2... 等模式?这对于短字符串来说可以,但我希望有一个更通用的解决方案(如果可能的话)?
I hope this title makes sense - I need case-insensitive regex matching on BlackBerry 5.
I have a regular expression defined as:
public static final String SMS_REG_EXP = "(?i)[(htp:/w\\.)]*cobiinteractive\\.com/[\\w|\\%]+";
It is intended to match "cobiinteractive.com/" followed by some text. The preceding (htp:w.) is just there because on my device I needed to override the internal link-recognition that the phone applies (shameless hack).
The app loads at start-up. The idea is that I want to pick up links to my site from sms & email, and process them with my app.
I add it to the PatternRepository using:
PatternRepository.addPattern(
ApplicationDescriptor.currentApplicationDescriptor(),
GlobalConstants.SMS_REG_EXP,
PatternRepository.PATTERN_TYPE_REGULAR_EXPRESSION,
applicationMenu);
- On the os 4.5 / 4.7 simulators and on
a Curve 8900 device (running 4.5),
this works. - On the os 5 simulators and the Bold
9700 I tested, app fails to compile
the pattern with an
IllegalArgumentException("unrecognized
character after (?").
I have also tried (naively) to set the pattern to "/rockstar/i" but that only matches the exact string - this is possibly the correct direction to take, but if so, I don't know how to implement it on the BB.
How would I modify my regex in order to pick up case insensitive patterns using the PatternRepository as above?
PS: would the "correct" way be to use the [Cc][Oo][Bb][Ii]2... etc pattern? This is ok for a short string, but I am hoping for a more general solution if possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
好吧,这不是解决一般问题的真正解决方案,但这种解决方法很简单、安全且高效:
当您在这里处理 URL 时,它们不区分大小写...
(我们写 google.com 或 GooGLE.COm 或其他什么都没关系)
最简单的解决方案(我们都喜欢 KISS_principle)是首先对输入进行小写(或者大写,如果您愿意),然后进行正则表达式匹配,无论是否区分大小写,因为我们确切地知道我们要做什么正在处理。
Well not a real solution for the general problem but this workaround is easy, safe and performant:
As your dealing here with URLs and they are not case-sensitive...
(it doesn't matter if we write google.com or GooGLE.COm or whatever)
The most simple solution (we all love KISS_principle) is to do first a lowercase (or uppercase if you like) on the input and than do a regex match where it doesn't matter whether it's case-sensitive or not because we know for sure what we are dealing with.
由于没有其他人回答了与
PatternRepository
类相关的问题,因此我将自行回答,以便可以关闭它。一种方法是使用如下模式: [Cc][Oo][Bb][Ii]2[Nn][Tt][Ee][Rr][Aa][Cc][Tt][Ii] [Vv][Ee]...等等,对于字符串中的每个字母,您可以放置 2 个选项。幸运的是我的绳子很短。
这不是一个优雅的解决方案,但它确实有效。不幸的是,我不知道如何修改传递给
PatternRepository
的字符串,并且我认为使用(?i)
修饰符时发生的崩溃是 BB 中的一个错误。Since nobody else has answered this question relating to the
PatternRepository
class, I will self-answer so I can close it.One way to do this would be to use a pattern like: [Cc][Oo][Bb][Ii]2[Nn][Tt][Ee][Rr][Aa][Cc][Tt][Ii][Vv][Ee]... etc where for each letter in the string, you put 2 options. Fortunately my string is short.
This is not an elegant solution, but it works. Unfortunately I don't know of a way to modify the string passed to
PatternRepository
and I think the crash when using the(?i)
modifier is a bug in BB.使用 jakarta 正则表达式库的端口:
https://code.google.com/p/regexp-me/
如果您使用 unicode 支持,它会消耗内存,
但如果你只想不区分大小写的匹配,
您只需在编译正则表达式时传递 RE.MATCH_CASEINDEPENDENT 标志即可。
新 RE("yourCaseInsensitivePattern", RE.MATCH_CASEINDEPENDENT | OTHER_FLAGS)
Use the port of the jakarta regex library:
https://code.google.com/p/regexp-me/
If you use unicode support, it's going to eat memory,
but if you just want case insensitive matching,
you simply need to pass the RE.MATCH_CASEINDEPENDENT flag when you compile your regex.
new RE("yourCaseInsensitivePattern", RE.MATCH_CASEINDEPENDENT | OTHER_FLAGS)