Dumitru Gutu
Dumitru Gutu

Reputation: 579

regular expression mysql REGEXP

I have the following strings in the table:

activity_result.step1.desires
activity_result.step1.desires.one
activity_result.step1.desires.one.1
activity_result.step1.desires.two
activity_result.step1.desires.abs
activity_result.step5.habits
activity_result.step2.noroc
activity_result.step2.habits.ops
activity_result.step2.habits.ops.trei

I want to use REGEXP in MySQL to get just the following records:

activity_result.step1.desires
activity_result.step5.habits
activity_result.step2.noroc

I started with ^activity_result\.step([0-9]|[1-9][0-9])$. Please help how to change the reg to get the desired result?

Upvotes: 0

Views: 94

Answers (1)

CollinD
CollinD

Reputation: 7573

Looks like you want this:

  • beginning of string (^)
  • activity_result (activity_result)
  • a period (.)
  • the word step (step)
  • a number between 0 and 99 without padding ([0-9]|[1-9][0-9])
  • a period (.)
  • any string that does not include a period ([^.]+)
  • end of string ($)

So in regex that's

^activity_result\.step([0-9]|[1-9][0-9])\.[^.]+$

You can test this out over at https://regex101.com/

Paste in the regex, add the gm modifier, and paste in your sample data and it will show you the matches.

Upvotes: 1

Related Questions