-3

Single regx for all these Conditions 1.Should allow only aphanumeric 2. along with only one space between words 3. Should allow only special characters like -.,' 4. Should not allow leading space, trailing space and consecutive blank space.

Valid input:

"testing with 2 regx solution"

Invalid input:

" testing    with 2 regx solution" or "testing  %^with 2 regx solution "
2

2 Answers 2

6

Try this

^(\w+\s)*\w+$
^     Start of string
(     Start of group
\w+   Word of one or more characters
\s    White space
)     End of group
*     Zero or more of the preeceding group
\w+   Word of one or more characters
$     End of string
2
  • 1
    Actually, your regex will not match abc (a sole word). Not sure if it is a requirement of the questioner though - he does not explicitly mention this case - the question sounds like the regex need to match a sole word for me :)
    – luiges90
    Dec 6, 2012 at 15:25
  • 1
    @luiges90 Changed the + to a * for the group quantifier to allow matching of a sole word, as I agree it may be needed. They are interchangeable depending on the requirements of the original poster.
    – rrrr-o
    Dec 6, 2012 at 15:32
-1
 inputString= Regex.Replace(inputString.Trim(),@"\s+"," ");

--SJ

1
  • 2
    The question asks how to match "strings with exactly one space between each word and no trailing or leading spaces" not modify strings to meet those criteria.
    – rrrr-o
    Dec 6, 2012 at 14:54

Not the answer you're looking for? Browse other questions tagged or ask your own question.