2

could someone help me out with a piece of regex please? I want to stop the user entering any charachter other than a-z or a hyphen -

Hope someone can help me.

Thanks

5
  • 1
    Are you sure you need a regex? It seems to me that you can simply check each character that is entered. I.e. if (ch >= 'a') etc. Commented Sep 27, 2010 at 12:35
  • You don't seriously post such a thing as a question to bother people? Reading a simple regex tutorial on the internet can answer you that questions in less then 10 minutes. Google helps! (and yes, I consider your question harmful) Commented Sep 27, 2010 at 12:38
  • [^a-z-] matches any character other than a-z and hyphen. Commented Sep 27, 2010 at 12:43
  • 1
    @ErikB chill out. Many questions here are much worse than this one. He asked nicely and clearly phrased out what he wanted. Sometimes inexperienced programmers are overwhelmed by what Google returns and would like the opinion of a human being. That's part of what this site is for, isn't it? Commented Sep 27, 2010 at 12:54
  • @seanizer: true, true. But that is so simple, if you just try to find out what a regex is you should already know enough to produce that regex yourself. Now he probably still does not really know what a regex is. He still has to read a tutorial. And that tutorial will still answer his question before he even read all of it. Commented Sep 27, 2010 at 13:43

4 Answers 4

7

You can use the regex: ^[a-z-]+$

  • ^ : Start anchor
  • $ : End anchor
  • [..] : Char class
  • a-z : any lowercase alphabet
  • - : a literal hyphen. Hyphen is usually a meta char inside char class but if its present as the first or last char of the char class it is treated literally.
  • + : Quantifier for one or more

If you want to allow empty string you can replace + with *

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

3 Comments

Now here's an explanation that the OP will probably understand. Nice (+1)
Thank you very much, i find regex really difficult and i couldnt think of any other approach to solving this manually :/ (i guess inexperience is the problem there ;) )
@tom don't let that get you down. Regular Expressions are awful beasts. It takes some programmers years to understand them. You'll figure it out eventually if you try. Here is a Java Online Regex Tester: fileformat.info/tool/regex.htm . You might want to play around with that, it might help you understand things better
2

If the string doesn't match ^[a-z-]*$, then a disallowed character was entered. This pattern is anchored so that the entire string is considered, and uses a repeated character class (the star specifies zero or more matches, so an empty string will be accepted) to ensure only allowed characters are used.

2 Comments

I think hyphen (-) should be escaped.
@Faisal the hyphen does not need to be escaped in a character class if it is the last character.
2

If you allow uppercase use this:

^[A-Za-z-]+?$

otherwise:

^[a-z-]+?$

Comments

2

If a string matches this regex ^[a-z\-]*$ then its fine.

2 Comments

@Tingu it is not necessary to escape a hyphen in a character class if it is the last character in it. Take a look at the section called "Metacharacters Inside Character Classes" in this article
If string matches this regex, then its fine - ain't it? Btw, hyphen need not be escaped when it is the first/last character in a character class @Tingu

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.