4

Regexp in Java I want to make a regexp who do this verify if a word is like [0-9A-Za-z][._-'][0-9A-Za-z] example for valid words

A21a_c32 
daA.da2
das'2
dsada
ASDA
12SA89

non valid words

dsa#da2
34$

Thanks

0

3 Answers 3

2

^[0-9A-Za-z]+[._'-]?[0-9A-Za-z]+$ (see matches on rubular.com)

Key points:

  • ^ is the start of the string anchor
  • $ is the end of string anchor
  • + is "one-or-more repetition of"
  • ? is "zero-or-one repetition of" (i.e. "optional")
  • - in a character class definition is special (range definition)...
    • unless it's escaped, or first, or last
  • . unescaped outside of a character class definition is special...
    • but in a character class definition it's just a period

References

Sign up to request clarification or add additional context in comments.

4 Comments

yes, i understand ... the sign + it's not ok, like in my example (ASDA) the special chars ._-' can miss btw, thanks for your answer
@tinti: I don't understand your comment. Are you saying there's something wrong with my pattern? If so, then let's fix it. Go back and forth with me on rubular. Give me input that should/shouldn't be matched.
Correction: ? is "zero-or-one", not "zero-or-more". "zero-or-more" is *
good explanation and a great reference site. I love a good regex. How about one for pure speed: ^(?>[0-9a-z]*)([-.'][a-z0-9])?(?>[a-z0-9]*)$ The (?>) stuff are possessive quantifiers alternate syntax. Java would support the normal syntax too: ^[0-9a-z]*+([-.'][a-z0-9])?[a-z0-9]*+$ (untested version) This is hardly worth it for small strings, but for large ones, avoiding excessive backtracking can make a HUGE diff.
1

If [._'-] are optional, put the ? with the next characters, like this:

[0-9A-Za-z]+([._'-][0-9A-Za-z]+)?

Comments

1
"(\\p{Alnum})*([.'_-])?(\\p{Alnum})*"

In this solution I assume that the delimiter is optional, the empty string is also legal, and that the string may start/end with the delimiter, or be composed only of the delimiter.

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.