0

I'm new to Regex in PHP. I suspect this will have a quick answer.


I have:

"Field A: result xyz, Field B: result123, Field C: and this is another result "


I need:

"Field A:result xyz,Field B:result123,Field C:and this is another result "


...so basically I want to:

  • remove all double spaces, and,
  • remove all single spaces if preceded by : or ,

My RegEx:

  • Pattern: ([ :,])\s+
  • Replace: ${1}

...works in Notepad++:

img1


...and seems like it would work on regex101.com (if I knew how to actually replace on there):

img2


...but in PHP with preg-replace, different variations of the pattern I've tried are telling me:

Unknown modifier '[' or Unknown modifier '\' or Unknown modifier '+'


...and I'm confident would work in VBA too. Here's the cheatsheet I use but obviously it doesn't all apply to PHP...

Thoughts, please & thanks?

ashleedawg
  • 20,365
  • 9
  • 72
  • 105

0 Answers0