Regex match count of characters that are separated by non
I want to count characters, but they may be separated by characters that do not match.
Here is an example. I want to match a text that has 10 or more word-characters. It may include spaces but i don't want to count the spaces.
Should not match: "foo bar baz" (should count 9)
Should not match: "a a" (should count 2)
Should match: "foo baz bars" (should count 10, match whole string)
This is what i came up with, but it counts the whole thing:
((?<=s)*w(?=s)*){10}
Edit I do not want to include spaces for counting. Sorry I edited this a few times, I didn't describe it correctly.
Any ideas on this?
Hey I think this would a simple but working one:
( *?[0-9a-zA-Z] *?){10,}
Breaking the regex down:
( *? --------It can start with space(s)
[0-9a-zA-Z] -Followed with the alphanumeric values
*?) ---------It can end with space(s)
{10,} -------Matches this pattern 10 or more times
Key: When I look at the count for regexes, it applies to the group, ie, the things in the brackets " ()
", this case, multiple spaces followed ONE from the alphanumeric values followed by spaces are still counted as one match. Hope it helps. :)
使用每个单个字符占用空格的组,并计算组:
^(s*w){10,}s*$
Using JS: Remove the spaces, then do the w
check
'foo baz barz'.replace(/ /g,'').match(/w{10,}/) != null //true
'foo bar baz'.replace(/ /g,'').match(/w{10,}/) != null //false
Match phone numbers in text:
var test = 'something foo baz barz 07999-777-111 and 01234 567890 01234567890 some more'.match(/(((?0d{4})?[ -]?d{3}[ -]?d{3})|((?0d{3})?[ -]?d{3}[ -]?d{4})|((?0d{2})?[ -]?d{4}[ -]?d{4}))([ -]?#(d{4}|d{3}))?/g);
//result: ["07999-777-111", "01234 567890", "01234567890"]
链接地址: http://www.djcxy.com/p/13410.html
上一篇: 正则表达式匹配包含“on”JS触发器的HTML标记
下一篇: 正则表达式匹配由非分隔的字符数