Hi
Is there any simple way or online website that can convert The numerical string like"123-322-14365" to regular expression?

Recommended Answers

All 10 Replies

\d{3}-\d{3}-\d{5}

A regular expression defines the rules by which a string can be analysed as conforming or not conforming to those rules. A numeric sample string does not define what the rules should be, it's just one example - eg we know that "123-322-14365" is valid, but what about "12-322-14365" etc etc.
So the answer to your question as asked is "no, it's not logically possible"

Maybe you mean something like "Is there any simple way or online website that can convert a rule like '3 digits, a hypen, three digits, another hypen, 5 digits' to regular expression?" In which case there are many many web sites that explain how a simple rule like that can be coded as a regex.

commented: Well sure, if you want to use words. +15

I have optimised this even more so you don't need to worry about those pesky patterns and stuff.

Just use (.*).

commented: Hah +15

In Access Data ACE Certification there is an question:

1.The numerical string "123-422-17365" would normally be found by which regular expression?

The right answer was:
(\d{3}[- ]){2}\d{5}

I wonder how its calculate it ?

It is not right answer - redundant white space after hiphens
right is: (\d{3}[-]){2}\d{5} (three digits with followed hiphens) {two times} and then five digits

My comment is the question itself needs work. I've never calculated what the regular expression would be. Maybe English isn't the first language here so try this instead. Don't omit question details. If you are presented the question, don't rewrite it. I see you revealed the real question later.

It's a really bad question. "123-422-17365" is just one string. A valid answer would be
"123-422-17365" - thats a regex that matches the string.
If you want to ask about a regex then at the very least you need a few examples of valid and invalid strings.

commented: Yup, that works too. +15

I agree that the OP worded it poorly but I think it was a per(f)ectly valid to assume that

string like"123-322-14365"

meant "three digits dash three digits dash five digits". When someone asks me if I have the correct time I generally don't just respond "yes" ^_^

I think it was a per(f)ectly valid to assume that ... meant "three digits dash three digits dash five digits

Yes, sure.
It's just that I've seen so many failed implementations based on techies making apparently valid assumptions about an incomplete user requirement :)

I've had a few of those. I learned that end-users can be very lazy when it comes to saying what they want so I would write up a spec and email it back with "is this what you want?" If they refused to commit then I would just work an anything else. It's frustrating to deal with "That's what I asked for but it's not what I want."

commented: Been there, coded that, delivered, "Hey it works just like I asked but not what I wanted." +15
commented: This is exactly why as my career develops I lean more and more strongly on the Gherkin syntax +9
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.