Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RegExp - How to check if a string contains a substring in the certain position?

I tried to search this question first but didn't found what I need, here is it:

I have a string like: "substring1/substring2.substring3" (e.g. "library/History.read")

I need a regular expression to check:

  1. the substring1 must be "library"
  2. the substring2 must have a captial letter at the beginning
  3. the substring3 must be "read"

I'm not familiar with regular expression, the current I wrote is: 'library/([a-zA-Z])\\.(read)' but it is not correct. Please help me, thanks!

like image 905
Meilan Avatar asked Nov 01 '25 14:11

Meilan


1 Answers

There are many ways to solve regex problems, starting from your own attempt here's something that works! :-)

library/([A-Z][a-zA-Z]+)\.(read)

You had three small mistakes:

  • . is a character class matching any character except newline. Escape your . like this \. not like this \\.; and
  • Your first capture group was matching exactly one letter. You missed the quantifier. Use the + qunatifier to match one or more.
  • As pointed out by Peter, you were missing a character class for your first capital letter
like image 136
Wanadi3929 Avatar answered Nov 04 '25 05:11

Wanadi3929



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!