开发者

How do I build an ICU regex to match this kind of string?

Hi I need an ICU regex that I think it is pretty basic but I don't know how to build it right. The regex should match strings like:

font-size: 9pt;
font-size: 15pt;
font-size:2pt;
font-size:22pt;

I'm trying to make something like this but it does开发者_StackOverflow社区n't work:

regex = \bfont\-size: [0-9]{3}pt;\b

I'm really new to regex so I'm not sure what am I doing wrong here. Any help is much appreciated.

P.S.: Does anyone know a good resource to get the hang of this fast?


font\-size\: ?[0-9]{1,3}pt\;

Should do the trick. Essentially, escape all non-alphanumeric characters (just to be on the safe side). Also, {1,3} means repeating 0-9 from one to three times, instead of always three times.

Edit: Updated the above regex. The trailing \b was removed, and the space before the number was made optional using ?.

Python demonstration:

>>> import re
>>> s = """
... font-size: 9pt;
... font-size: 15pt;
... font-size:2pt;
... font-size:22pt;
... """
>>> re.findall("font\-size\: ?[0-9]{1,3}pt\;", s)
['font-size: 9pt;', 'font-size: 15pt;', 'font-size:2pt;', 'font-size:22pt;']


Two problems I see with your regex:

  1. {3} matches exactly three things. You probably want {1,3} to match 1 to 3.

  2. I don't think \b is going to do what you want right after a semicolon. Perhaps you want something like \s* (zero or more whitespace).

If you want to learn regexes fast, your best bet might be to use a regex debugging tool and experiment.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜