0

I tried using this pattern

^[A-z]*[A-z,-, ]*[A-z]* 

To match against a string that starts with multiple alpha characters (a-z) followed by multiple hyphens or spaces and ends with alpha characters, eg:

Azasdas- - sa-as

But it does not work.

2
  • 3
    Questions usually have a ? in them. It's nice that you're sharing a regex that doesn't work, but this isn't a place for sharing broken code. Most people will ask a question as to why it's broken and "could you help?" type thing. Commented Oct 23, 2011 at 16:07
  • @MarcB: The OP summarized the problem and told us how he's tried to solve it; what more do you need? "What am I doing wrong?" is implied, and "Can you help?" is a waste of space (IMO). Commented Oct 23, 2011 at 17:50

4 Answers 4

1

Try ^[A-Za-z][A-Za-z -]*[A-Za-z]$

^ indicates that the word should start with alphabets (A-Z or a-z) and then followed by any number of alphabets or hyphens. And then end with alphabets denoted by $ .

Also, you should not be using A-z because this will include unintended characters from ASCII range 91 to 96. See this table

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

2 Comments

This leaves out places where spaces are allowed.
@DaveNewton thanks :) made the necessary changes to include spaces.
1

Don't use ',' (comma)

^[A-z]*[A-z- ]*[A-z]*

Comments

1

You don't want the commas, in a character range you also need to specify [A-Za-z\- ] because the ASCII for A-Z and a-z aren't contiguous. You're missing some allowable spaces, and your last expression needs to account for the hypen.

You need something closer to this:

^([A-Za-z]*)-\s*([A-Za-z][A-Za-z -]*)([A-Za-z-]*)$

Depending on how you actually want to break things up. Without knowing the context behind the "chunks", it may or may not just be easier to split it apart on hyphens.

Edit

Actually, it's more like:

^([A-Za-z]*)([- ]*)([A-Za-z-]*)$

This is a word, followed by arbitrary spaces and hyphens, followed by a word that may contain a hyphen.

8 Comments

@NullUserExceptionఠ_ఠ Wouldn't that allow non-letter chars in the first word?
You're like a ninja! (Yeah, I pasted the wrong string in--was just editing it.)
If you place the - at the end or start of a character class, it loses its special meaning.
@NullUserExceptionఠ_ఠ Yeah, I just like to make it explicit to avoid any confusion.
It just boils down to personal taste, but I prefer to escape as few characters as possible. Especially in Java, which forces you to double escape regex strings.
|
1

The currently accepted answer (^[A-Za-z][A-Za-z-]*[A-Za-z]$) will only match strings that are at least two characters long--for example, it will match the string "AB", but not just "A" or "B". Compare that to this regex:

^[A-Za-z]+([ -]+[A-Za-z]+)*$

By grouping the [ -]+ and the second [A-Za-z]+ together I'm saying, if there are any spaces and/or hyphens, they must be followed by more letters. The * quantifier on the group makes it optional, so "A" will match, while still meeting the requirement that the string start and end with a letter.

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.