regex: matching only first occurrence per line

To match the first occurrence on every line you need to anchor the pattern to the start of the line.

See regex in use here

^([^-]*?)\s*-\s*
  • ^ Assert position at the start of the line
  • ([^-]*?) Capture any character except - any number of times, but as few as possible into capture group 1
  • \s*-\s* Match any number of whitespace characters, followed by the hyphen - character, followed by any number of whitespace characters

Replacement: $1, '

The token $1 is a reference to the text that was most recently captured by the first capture group.


If you find yourself with a similar problem but cannot fix it using accepted answer, try this:

I was trying to remove the first

','

from an sql exported data file, so as to merge people name with their surnames, example of data: ,

('MARTIN','MULLER QUEIJO',46574180,'1996-06-12','Dirección no definida','Dirección no definida','',3)
,('ALFREDO','ACOSTA',342,'23423','asdas','asdasd','',3)
,('JULIO','RODRIGUEZ',3223424202569,'42423','23fs','asdas','',3)
,('EVELIS (43)','MAIDANA CASCO',2342,'asdas','dfgdfg','3ggfd','',3)

To do so I ended up using this regex:

(',')(?=.*(',').*(',').*(','))

As you can see I get the first occurrence of my desired token (',') and discard the same token as many times as you predict it to appear in each line. SO this may only work if you already know how many times this token appears in the lines, and as long as it is the same amount of times for avery line (4 times in this case, so I removed it 3 times).

Tags:

Regex