Home > database >  How do I provide the second match in my regular expression and do full validation?
How do I provide the second match in my regular expression and do full validation?

Time:03-24

I have regular expressions like below. Ancak tam olarak doğru bir şekilde regex yazamadım.

keys:value,keyx:values,keyt:valus,........ OR keys:value

How can I perform an exact match both ways?

regex: (^\w :\w |(,))

CodePudding user response:

For a single or multiple occurrence of the key:value parts and matching only word characters, you can match the first occurrence and optionally repeat a comma and then then same pattern using a repeating group.

^\w :\w (?:,\w :\w )*$
  • ^ Start of string
  • \w :\w Match the first key:value occurrence
  • (?:,\w :\w )* Optionally repeat using * a comma and again a key:value pair
  • $ End of string

See the matches on regex 101.

  • Related