Regular Expressions 101

Community Patterns

Add a string to a comma-separated list if not already in list

1

Regular Expression
PCRE (PHP <7.3)

/
^(text[ ]*=[ ]*)'((?(?!item_1[,]?).)*)'
/
gm

Description

  • matches only lines that do not contain the string
  • uses negative look arounds
  • appends the new item always to the back
  • suffers from a leading comma in case the list was empty
Submitted by @ubunatic - 8 years ago