-2

I am looking for a regex that validates the following string c3a0acfb-cdce-4a5c-a089-43c09578fc0f, it always will have lower case letter, number and -, I know that [0-9a-z] validates the letters and numbers, but I need to say that caracter - can appear anywhere.

Can anyone help me, please?

Thanks.

7
  • 4
    Have you tried anything yet? Commented Jul 3, 2013 at 13:27
  • ^[0-9a-z]{8}-([0-9a-z]{4}-){3}[0-9a-z]{12}$ Commented Jul 3, 2013 at 13:33
  • Can check the regex easily too if ind this place helpful regexplanet.com/advanced/java/index.html Commented Jul 3, 2013 at 13:34
  • 3
    Is it a hexadecimal code? If so you should validate only for [0-9a-f] not [0-9a-z] Commented Jul 3, 2013 at 13:35
  • To those trying to give a regex placing the -s at specific locations in the string, note that the OP says: "caracter [sic] - can appear anywhere". The comments on Bathsheba's answer confirm this further. Commented Jul 3, 2013 at 13:38

5 Answers 5

3

regex :

([a-z0-9\-]+)

The above will allow alphabets digits and hyphen.

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

2 Comments

You don't need to escape the -: stackoverflow.com/questions/7604879/…
Clarification (so that it's not dependent on the link): You don't need to escape the - if it's the first or last character listed in the character set.
2

Assuming this is a GUID, use this:

"{0,1}([0-9a-fA-F]){8}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){12}"

4 Comments

I guess I was not very clear in what I wrote, I am sorry. I wanted to say my string can have lower cases letters, numbers and -, not necessary in that order neither quantity that I wrote in my question, do you understand?
I do but was thinking that what you really wanted was a reg-ex for a globally unique identifier.
Your answer helps me too, because I saw that I can check the length of string between hyphens. Thanks.
@Danilo M, you can also use {a,b} to mean must have between a and b (inclusive) occurrences of the previous.
1
([0-9a-z]*)\-([0-9a-z]*)\-([0-9a-z]*)

This regexp should do it, in java you might need to add an extra backslash before -

Assuming that it is a certain number of -

In the future you can experiment at: http://gskinner.com/RegExr/

Comments

0

This is so simple!! Have you really tried anything?? If you want to match a string containing lowercase letters, digits and hyphen, you can use:

[a-z0-9-] 

If you strictly want to match the above pattern, then:

 (([a-z0-9]+)(-)?)+ 

should work. It means any alphanumeric folled by one or zero hyphen.

I am not sure if for hyphen we need to use /- instead of - directly :D. So atleast do this by yourself and let me also know :D.

[a-z0-9-]+ will also work for you, but it will also accept strings of type:

 adcacf--sadcac-das-----

and even this will also be accepted:

------------- (strings of hyphen)

So, if you know that you won't get these type of strings, then you may go with [a-z0-9-]+ as told by PVR.

Comments

0

Try [0-9a-z\\-]. (Warning: This doesn't validate the length of the string.)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.