0

I would like to generate a regular expression that matches with a room description like this:

My Room Description(enter, n, s, e and w)
My Room Description(enter, e, s, w and n)
My Room Description(s, w, e, n and enter)
My Room Description(n, e, w, s and enter)

The exit directions could be in different position but will always be the same amount (4) in this case.

This should not match:

My Room Description(n, up, e, w, s and enter)

because it has 5 exits (other than “enter”).

Bohemian
  • 412,405
  • 93
  • 575
  • 722
relez
  • 750
  • 2
  • 13
  • 28
  • Would this do: `"^My Room Description\(.*?\)( or)?`? How many lines can there be that match this that aren't the one you want? – Bohemian Feb 22 '19 at 03:17
  • Its only this line, but it could be another with the same description but more exits: ex: My Room Description(n, e, w, s, enter and up), notice that this one has an extra exit: up. In this case, I would like to ignore the last one. – relez Feb 22 '19 at 03:24

1 Answers1

0

Try this:

^My Room Description\((\w+(, | and )?){5}\)$

See live demo.

——-

To allow optional leading text, such as "HP:245 EP:245 >> " as per your comment:

^[\w: >]*My Room Description\((\w+(, | and )?){5}\)$

Or more strict:

^([\w: ]* >> )?My Room Description\((\w+(, | and )?){5}\)$

This restricts the leading characters to only those you’ve provided as an example. To allow anything:

^.*My Room Description\((\w+(, | and )?){5}\)$
Bohemian
  • 412,405
  • 93
  • 575
  • 722
  • Slightly simpler: `^My Room Description\((\w+, ){3}[^,]*\)` - only three commas in a match – Mark Feb 22 '19 at 04:12
  • @Mark depends if you want to assert the syntax, or allow almost anything. – Bohemian Feb 22 '19 at 04:40
  • Sometimes my room description looks like this: My Room Description(enter, n, s, e and w), and sometimes looks like this: HP:245 EP:245 >> My Room Description(enter, up, s, w and n). I am using this one for the second case: ^.*>> My Room Description\((\w+(, | and )?){5}\)$. Now, is there any way to match both description (with and without the HP:245 EP:245 >>)? Thanks! – relez Feb 22 '19 at 04:54
  • @mark surround text with backticks to format as code (which retains escapes). I edited your comment above into add such backticks. – Bohemian Feb 22 '19 at 05:18