Improve regular expression

I want to match something like this in PHP:

class-11/xxx/xxx/xx/xxx/things_to_remember/
class-12/xxx/xxx/xx/xxx/things_to_remember/

However I don't want to match something like this:

xxx/class-11/xxx/
class-11/xxx/things_to_remember/xxx
class-11/xxx/

I am writing it like this:

^(class-[12]{2})/.+/things_to_remember/$

I heard regular expression have many features like greedy etc. and they also need to be efficient ? Is the above regualar expression good ?

Answers


I wrote a little regex here that captures it like the format you wrote. It might need some slight changes as I didn't know how many digits could be in after class.

/
class-(\d{2}) #Matches class, makes sure that class only is 2 digits - captures class digits
\/([^\/]{3}) #captures first 3 characters that aren't a slash.
\/([^\/]{3}) #Capture 3 characters again.
\/([^\/]{2}) #captures two characters
\/([^\/]{3}) #Captures 3 characters
\/things_to_remember\/ #Matches last piece of string.
/xg

You can test it out here.


Need Your Help

Shiny scoping rules - where to load libraries in modular architecture

r module shiny package

With this question I'm only interested in obtaining some clarity on a best approach to using packages while working with a Shiny app. Despite the fact that, contrary to good practice on asking R-re...

Is it a good idea to make Ansible and Rundeck work together, or using either one is enough?

jenkins ansible continuous-delivery devops rundeck

Recently I'm looking at Ansible and want to use it in projects. And also there's another tool Rundeck can be used to do all kinds of Operations works. I have experience with neither tool and this i...