42

I am looking for a regex where i have one or more words (possibly should cover alpha numeric also) separated by spaces (one or more spaces)

  1. " Test This stuff "
  2. " Test this "
  3. " Test "

Above are some examples of it

I wrote a regex to see what is repeating for #1

\s*[a-zA-Z]*\s*[a-zA-Z]*\s*[a-zA-Z]*\s*

so i wanted to do something like {3} for repeating section.

But it does not seem to work.. I cant believe it is this difficult.

(\s*[a-zA-Z]*){3}
HamZa
  • 14,671
  • 11
  • 54
  • 75
user2921139
  • 1,669
  • 4
  • 17
  • 25

2 Answers2

66

If you do not care just how many words you have, this would work:

[\w\s]+

\w is any alphanumeric. Replace it with a-zA-Z if you need only letters.

mvp
  • 111,019
  • 13
  • 122
  • 148
  • 0001 :: Test This Stuff :: #### PASS =====> This is the text that I am writing a regex for? I wrote a regex here which does not seem to work - i want to write a generic one instead of a specific one "if re.search('\d\d\d\d\s::\s[[a-zA-Z]\s]+::\s\#\#\#\#\s[A-Z]*', aline):" – user2921139 Aug 05 '14 at 21:39
  • The number of words can vary..which is why I want a generic one. "Test This Stuff" or "Test" or "Test Stuff".. something like that. – user2921139 Aug 05 '14 at 22:07
  • 1
    This regex should work for your purpose. However, your regex engine may need to wrap it in `^$` for your tests to pass, like this: `^[\w\s]+$` – mvp Aug 05 '14 at 22:32
  • But the ^ would match the very beginning of the line.. wouldnt it? – user2921139 Aug 05 '14 at 22:34
  • Yes, `^` means that it must start from the beginning - use it if you need to check that whole string contains only words with spaces and nothing else. Check this [fiddle.re link](http://fiddle.re/awq7z) (click on Java test) – mvp Aug 05 '14 at 22:38
  • "if re.search('\d\d\d\d\s::\s[[a-zA-Z]\s]+::\s\#\#\#\#\s[A-Z]*', aline):" --- this dosent work for me!!?! – user2921139 Aug 05 '14 at 22:40
  • You can't use `[[a-zA-Z]\s]` - this is invalid syntax. Use `[a-zA-Z\s]` – mvp Aug 05 '14 at 22:42
  • MVP - that did not work either "if re.search('\d\d\d\d\s::\s[a-zA-Z\s]+::\s\#\#\#\#\s[A-Z]*', aline)" – user2921139 Aug 05 '14 at 23:16
  • You should provide more information in your question: what is your input and what is your expected output - right now we can only guess on what is your goal, because you are not telling whole story – mvp Aug 05 '14 at 23:25
  • Duh.. i was not grouping in the script correctly.. Thanks that's the answer! – user2921139 Aug 05 '14 at 23:39
  • be careful, because `[\w]` will catch `\n` as well. – belgoros Nov 07 '17 at 10:20
  • @Javix: `[\w]` will not catch newline. `\s` will, you can replace it with just literal space. – mvp Nov 07 '17 at 16:10
  • @mvp Yep, my bad, exactly. – belgoros Nov 07 '17 at 16:25
  • Thanks, best way for me – Swetabja Hazra Mar 07 '20 at 19:17
13

I think you need something like this,

^\s*[A-Za-z0-9]+(?:\s+[A-Za-z0-9]+)*\s*$

DEMO

OR

^(?:\s+[A-Za-z0-9]+)+\s+$

DEMO

Avinash Raj
  • 172,303
  • 28
  • 230
  • 274
  • 2
    I like your answer the most because it doesn't match empty strings, such as, `' '` (ie, a bunch of spaces, apostrophes not included, just for visualization). Right now it does validate for things like: `' this is a string with spaces before'`, but this case could be invalidated by removing the `\s` at the beginning of the pattern (although I'm keeping it as it's useful). I was attempting my own solution to this, but was getting stuck--your use of the non-capturing group = . I added some other special characters for my own usage. Thanks for the helpful contribution! – twknab Nov 02 '17 at 02:34