tim-montague
tim-montague

Reputation: 17372

RegEx pattern to capture comma-separated list of words

Does anyone know a repeatable RegEx pattern to capture the words between a comma separated list?

For example:

City,State,Latitude,Longitude

// $1,$2,$3,$4

This is for Find and Replace in an IDE, so the .split method provided by most programming languages is not a solution. Also no look-aheads / look-arounds please, these are not universally supported.

Clarifications: (1) just need to capture comma-separated words on a single line - not across multiple lines, (2) a solution to the example would be enough.

Upvotes: 0

Views: 2642

Answers (3)

Xosrov
Xosrov

Reputation: 729

Try this with $1, tested with VsCode

\s?(\w+)\s?,?

Upvotes: 1

Emma
Emma

Reputation: 27723

If our inputs are as simple as that, your best solution is just fine, and ^ may not be necessary, and these expressions might work:

([^,]*),([^,]*),([^,]*),([^,]*)
([^,]+),([^,]+),([^,]+),([^,]+)

or if start and end anchors are necessary:

^([^,]*),([^,]*),([^,]*),([^,]*)$
^([^,]+),([^,]+),([^,]+),([^,]+)$

RegEx Circuit

jex.im visualizes regular expressions:

enter image description here

Upvotes: 1

tim-montague
tim-montague

Reputation: 17372

This is my best solution at the moment

^([^,]*),([^,]*),([^,]*),([^,]*)$

Upvotes: 0

Related Questions