Regular match matches for characters separated by inconsistent characters

I want to count the characters, but they can be separated by characters that do not match.

Here is an example. I want to combine text with 10 or more characters of a word. It may contain spaces, but I do not want to count spaces.

Do not match: "foo bar baz" (should be considered 9)
Should not match: "a a" (should be considered 2)
Must match: "foo baz bars" (should be 10, match the whole line)

This is what I came up with, but he considers everything:

((?<=\s)*\w(?=\s)*){10} 

Edit I do not want to include spaces for counting. Sorry, I edited this several times, I did not describe it correctly.

Any ideas on this?

+7
javascript ruby regex
source share
3 answers

Hey, I think it will be simple, but working:

 ( *?[0-9a-zA-Z] *?){10,} 

Regular expression violation:

  • ( *? --------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 counter for regular expressions, it applies to the group, i.e. things in brackets " () ", this case, several spaces following ONE of the alphanumeric values, followed by spaces, are still counted as one match. Hope it helps. :)

+7
source share

Use a group that consumes spaces with each char word and counts groups:

 ^(\s*\w){10,}\s*$ 
+4
source share

Using JS: Remove the spaces, then check \w

 'foo baz barz'.replace(/ /g,'').match(/\w{10,}/) != null //true 'foo bar baz'.replace(/ /g,'').match(/\w{10,}/) != null //false 

Match the phone numbers in the text:

 var test = 'something foo baz barz 07999-777-111 and 01234 567890 01234567890 some more'.match(/((\(?0\d{4}\)?[ -]?\d{3}[ -]?\d{3})|(\(?0\d{3}\)?[ -]?\d{3}[ -]?\d{4})|(\(?0\d{2}\)?[ -]?\d{4}[ -]?\d{4}))([ -]?\#(\d{4}|\d{3}))?/g); //result: ["07999-777-111", "01234 567890", "01234567890"] 
+1
source share

All Articles