I have some products in my table and each product has a unique number. Few examples are given below:
H-E223I22A1234
RW-E123I22B2345
E223I22A4545
Above codes are following below format:
(H|RW)- = Optional
E1|E2... = This will be any series of only 2 characters (Preferably we will follow aplhabet and number)
23 = This is current last 2 digit of current year
I = Alphabet representation of current month. In this I = September(09)
22 = Current date. it will be 01-30|31
A|B = It is Side of production. Will always be either A or B
1234 = 4 digit random number
Now I want to find out all products by series and current month and year. For example, If I want to find product of E2 series of September 2023 then what will be the MySQL query. I was trying to achieve the same with regular expression, but MySQL always return all records. Below is my regular expression:
Select * from products where code REGEXP '((H|RW)-)?(([A-Z]{1}[1-9]{1}){1})(23)(I)(0?[1-9]|[12][0-9]|3[01])[A|B](d*)'
2
Answers
You can build your regex from the component parts you are interested in. For example, using variables:
This results in
You can then get results by:
Output for your sample data:
Demo on dbfiddle.uk
You may want to add start (
^
) and end-of-string ($
) anchors to the regex if necessary.You can use this pattern for matching.
And, this pattern for capturing.