1

I am trying to match a string that start with the set word "hotel", then a hyphen, then a word of any length, then another hyphen and finally a number of any length.

Edit: Dima gave the solution I needed in the comments of this question! Thanks Dima.

Further edit: elaborating on Dima's answer, adding capturing groups making it easier to retrieve the information entered, and correcting the last bit to only accept digits:

^hotel-(.+)-(\d+)

4
  • 2
    What is wrong with "hotel-something"? Why is that not good? Commented Dec 23, 2014 at 18:35
  • 1
    This maybe: ^hotel-.+-.+ or equivalently `^hotel(-.+){2,} Commented Dec 23, 2014 at 18:43
  • Please keep the original question, so it can be useful for others. Commented Jul 7, 2017 at 20:11
  • Woops I didn't mean to delete the whole question, I was trying to make it more concise Commented Jul 7, 2017 at 20:15

3 Answers 3

2
^hotel-(.)*$

(But hotel-something WILL work, according to your initial statement).

So, if you actually want something like:

hotel-XXXXXX-YYYYYYY

Then the regex is :

^hotel-(.)*-(.)*$

Try a regex online tester like http://www.regextester.com/.

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

Comments

1

If you want to match the start of the input, you use ^.

so if you have ^hotel-\b, that will force hotel to be at the start of the string.

as a note, you can use $ for the end of the string in a similar way.

Comments

1
\bhotel-[^\s-]+-[^\s-]+\b

\b means that it should be a word boundery

[^\s-] means anything but - or whitespace

https://regex101.com/r/mH3vY8/1

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.