fke7985 发表于 2005-10-31 13:49:19

下面的正则表达式相同吗?如果不同分们分别匹配什么?

下面的正则表达式相同吗?如果不同分们分别匹配什么?
/^(a|b)*$/
/^a*|b*$/

pakix 发表于 2005-10-31 17:09:44

'ba' as example:
it match the first one but disagree with the following one.

the secend one means:
/(^a*)|(b*$)/

sorry but no chinese IME;-)

pakix 发表于 2005-10-31 17:38:46

sorry for mistake
the word I said above may be correct if U change '*' to '+'

here U use '*'
so let's use 'c' as example:
it dismatch the first RE cause it means: only 'a' or 'b' can exist from the begin of the line to the end.
and it match the secend RE cause the '*' can be zero

fke7985 发表于 2005-11-1 10:02:56

能不能用中文?

真的没有人能详细说明上面的两个正则的不同吗?

zhy2111314 发表于 2005-11-1 10:14:17

Re: 下面的正则表达式相同吗?如果不同分们分别匹配什么?

下面的正则表达式相同吗?如果不同分们分别匹配什么?
/^(a|b)*$/
^表示行首,$表示行尾,(a|b)表示a或者b匹配其中一个就可以,*表示前面一个char.set出现的次数为0次或多次

/^a*|b*$/

差别应该可以看出来了吧

pakix 发表于 2005-11-1 10:39:00

zhongwen mei you, pingying xing ma?-(

fke7985 发表于 2005-11-2 09:26:41

倒~~~~~~~~~~~~~

fke7985 发表于 2005-11-2 09:29:00

Re: 下面的正则表达式相同吗?如果不同分们分别匹配什么?

下面的正则表达式相同吗?如果不同分们分别匹配什么?
/^(a|b)*$/
^表示行首,$表示行尾,(a|b)表示a或者b匹配其中一个就可以,*表示前面一个char.set出现的次数为0次或多次

/^a*|b*$/

差别应该可以看出来了吧

这个我知道,可是很奇怪的是用/^a*|b*$/匹配任何字符串,可是匹配长度为0(当然字符串中不含a和b)但是他却有返回True,说有找到!我真晕了

zhy2111314 发表于 2005-11-2 12:29:08

*本来就是0个或者多个啊

fke7985 发表于 2005-11-3 09:33:32

*本来就是0个或者多个啊

你但是匹配的是0个或是多个a或是b, 比如我输入一个214123412这样我用
regx的模式串为/^a*|b*$/
regx.test("214123412")这句却返回TRUE,匹配长度虽然为0可是我都根据regx.test("214123412")这个的结果来判断是否找到啊,真是奇怪,即然长度为0又怎么会返回TRUE呢?

pakix 发表于 2005-11-11 19:27:04

here
RE"^a*" = RE"^"
RE"^" means all line(cause every line has a ^)

match means true whether its match count is zero

pakix 发表于 2005-11-11 19:28:03

RE"b*$" work the same way
页: [1]
查看完整版本: 下面的正则表达式相同吗?如果不同分们分别匹配什么?