0

I'm new to regex so I hope that there is an easy problem to spot in this. Basically I'm trying to check if user input matches a word or phrase containing letters or numbers.
([a-zA-Z0-9]+)|(([a-zA-Z0-9]+\s+[a-zA-Z0-9]+)+)\s* Hope it's not too messy, all help is very much appreciated. It works for a phrase like "the man is a" but not for "the man is a dog" which puzzles me.

2
  • Maybe you should simplify and simply use: ([\w\s]+) Commented Feb 23, 2014 at 16:17
  • Great resource here regular-expressions.info Commented Feb 23, 2014 at 16:21

2 Answers 2

2

It should work for both of your phrases and capture the both times, as your regex won't evaluate your second alternation, as the first one ([a-zA-Z0-9]+) will match every time (see here).

If all you want to do is match a word or phrase containing letters or numbers I'd use

(?:[a-zA-Z0-9]+\s?)+

demo @ regex101

I won't use \w instead of [a-zA-Z0-9], as \w matches on _ in many regex-engines, too.

Explanation:

(?:           #start of non-capturing group
  [a-zA-Z0-9] #character class matching one of a-z, A-Z and 0-9
  +           #between one and unlimited times
  \s?         #optional whitespace (includes tabs newlines)
)             #end of non-capturing group
Sign up to request clarification or add additional context in comments.

3 Comments

What the ":" after the ? mark used for in this Regex Basti? I always have a tough time with Regexs as well.
@AlvinBunk () is a usual capturing group, while (?:) is a non-capturing group, so to only group tokens and not capture them.
Great, I think this answers the OPs question.
0

If you want to just check for letters or numbers, you can simply do:

String regex = "([a-z]|[A-Z]|[0-9])+";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.