Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I am completely new to regex and I would greatly appreciate any help.

The task is simple. I have a CSV file with records that read like this:


I would like to replace the first comma with a space and leave the rest of the commas intact, for every line. Is there a regex expression that will only match the first comma?

I tried this: ^.....,. This matches the comma, however, it also matches the entire length of the string preceding the comma, so if I try to replace this with a space all of the numbers are deleted as well.

share|improve this question
what tool are you using? (sed, perl, awk, something else?) –  Mat Apr 5 '11 at 6:07
Textpad (Windows) –  cows_eat_hay Apr 5 '11 at 6:14
alt-mouseselect and press space? –  Terry Apr 5 '11 at 7:01

4 Answers 4

up vote 15 down vote accepted

The matching pattern could be:


That means

^        starts with
[^,]     anything but a coma
+        repeated one or more times (use * if the first field can be empty)
([^,]+)  remember that part
,        followed by a coma

In e.g. perl, the whole match and replace would look like:

s/^([^,]+),/\1 /

The replacement part just takes the whole thing that matched and replaces it with the first block you remembered and appends a space. The coma is "dropped" because it's not in the first capturing group.

share|improve this answer
Awesome! Thank you Mat, it worked great. It actually did not work in Textpad (I think their regex is limited), so I ended up downloading PowerGrep, and used the search and replace with the expression you provided and it worked great. Thanks also for the nice explanation, it helps understand what's going on. –  cows_eat_hay Apr 5 '11 at 7:15

This should match only the first number and the comma: ^(\d{5}),. If you'd like to gobble up everything else in the line, change the regex to this: ^(\d{5}),(.*)$

share|improve this answer
This also did the trick. I actually ended up using Mat's solution but I tested yours too and it works. Thanks for the help! –  cows_eat_hay Apr 5 '11 at 7:18
@cows_eat_hay: no problem, glad you solved your problem in the end. –  alex Apr 5 '11 at 12:44
s/,/ /

This, by default (i.e. without the g option), replaces only the first match.

share|improve this answer
Is this actually Textpad search&replace syntax? –  Daniel Beck Aug 1 '12 at 21:54
This is a syntax of sed, perl and some other tools. –  pabouk Dec 2 '13 at 20:57

Textpad recently upgraded their RegEx engine to handle Perl regular expressions. I needed to add the word 'TEST' to the end of a fixed-length set of data without lengthening the data.

The following variant, applied to selected text worked:

Search: ([^#]+)#### [Note, the # characters are actually spaces here.] Replace With: \1TEST

So, for example, 'BAFEIWRTBT,ITOPERQWQ################' became 'BAFEIWRTBT,ITOPERQWQTEST############'

Since I needed to apply this to selected text NOT at the start of the line, the leading ^ in the original suggestion was dropped.

share|improve this answer
Though the information about the upgraded support of regex in Textpad is useful I do not understand how is the rest of your answer related to the question. Could you please provide an answer(s) to the question using the updated regexs? –  pabouk Dec 2 '13 at 21:02

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.