-7

I want my string to match a pattern like this +cat,+dog,-name,+cat

So basically it should start with + or - followed by variable name and then ","

So I want to have a regex for the same for matching the string.

Could you please tell the regex for this pattern ?

user3678399
  • 89
  • 1
  • 11

2 Answers2

0

This regex matches one instance of "+/- plus a variable name":

[+-]\w+

Now, to make it so that it matches multiple instances of the above, separated by commas, change it like this:

[+-]\w+(,[+-]\w+)*

Basically, I added this part:

(,[+-]\w+)*

It is a group that can repeat zero or more times (*), in the group, there is a comma, followed by the first pattern we saw.

Basically, you just need to move the comma to the start of the capture group.

Sweeper
  • 213,210
  • 22
  • 193
  • 313
0

Try this ==> ^[+-]\w*[,]

Hope this help!

Nejib
  • 11
  • 1