You are here: PSPad forum > English discussion forum > Replace the first occurence of the text

Replace the first occurence of the text

Goto Page: 1 2 Next

#1 Replace the first occurence of the text

Posted by: zales33 | Date: 2014-12-12 14:43 | IP: IP Logged

Good morning,
I have opened my .csv file in PSPad and what I'm trying to do it add double quotes around the items in the first column. The file has about 300,000 rows and looks like this:

100000.00,,,100235,0,2345.98,CAD,November
ABDDGHEF.00,,10002533,,0,64529.53,CAD,December

As you can see the items in the first column are different lengths. I tried using the Alt key to select a rectangular block (the first column including the first comma), then using find(,) and replace (",) but it didn't work perfectly due to the variable length of the first column. I know how to use Edit > Lines Manipulation to add quotes at the beginning of each line but I don't know how to accurately add another quote before the first comma of each line. Does anyone have thoughts on how I can accomplish this? My goal is to have the lines look like this:

"100000.00",,,100235,0,2345.98,CAD,November
"ABDDGHEF.00",,10002533,,0,64529.53,CAD,December

Thank you for reading my post. I appreciate any help you might be able to offer!

Options: Reply | Quote | Up ^


#2 Re: Replace the first occurence of the text

Posted by: pspad | Date: 2014-12-12 14:51 | IP: IP Logged

Use regular expression to replace first occurence of number

Search: ^(.*?)\,
Replace: "$1",
[x]Regular expressions

Edited 1 time(s). Last edit at 2014-12-12 14:52 by pspad.

Options: Reply | Quote | Up ^


#3 Re: Replace the first occurence of the text

Posted by: zales33 | Date: 2014-12-12 15:16 | IP: IP Logged

that is purely awesome, thank you so much!!! I tried playing around with Regular Expressions but I couldn't figure out how to create the right syntax. Thank you so much for your help and fast response!!!

Options: Reply | Quote | Up ^


#4 Re: Replace the first occurence of the text

Posted by: pspad | Date: 2014-12-12 15:40 | IP: IP Logged

This expression search for anything up to first comma

Options: Reply | Quote | Up ^


#5 Re: Replace the first occurence of the text

Posted by: Andreas | Date: 2014-12-14 06:07 | IP: IP Logged

Search for "regex tester" to find an online regex tool (e.g. regex101.com) and use PSPad plugin phreplace www.phdesign.com.au for better and easier multiline search and replace.

Options: Reply | Quote | Up ^


#6 Re: Replace the first occurence of the text

Posted by: zales33 | Date: 2014-12-15 17:00 | IP: IP Logged

Hello,
Thank you for all your responses, they have helped immensely. Since my last post, I have run into a problem. I now need to replace the 5th comma with a ," and the sixth comma with a ",

I have been using the regex tester that Andreas suggested, it is a very helpful tool but I still cannot figure out how to make regex match the 5th or 6th occurrence of a character. If anybody reading this post has any suggestions, I would really, really appreciate it!

Kind Regards.

Options: Reply | Quote | Up ^


#7 Re: Replace the first occurence of the text

Posted by: pspad | Date: 2014-12-15 18:22 | IP: IP Logged

search: ^(.*?\,.*?\,.*?\,.*?\,.*?\,)(.*?)\,
Replace: $1"$2",

Options: Reply | Quote | Up ^


#8 Re: Replace the first occurence of the text

Posted by: zales33 | Date: 2014-12-15 20:22 | IP: IP Logged

no wonder I didn't figure it out. I never thought of using an approach like that. Thank you so much! This is an amazing tool that I wish I had more knowledge about. Might have to do some Googling or go to the librarysmiling smiley

Thank you again, I really appreciate it!!!

Options: Reply | Quote | Up ^


#9 Re: Replace the first occurence of the text

Posted by: pspad | Date: 2014-12-15 20:35 | IP: IP Logged

Hello.
In my previous example we replaced content up to first comma. I did nothing else than copy this expression to cover content up to 5th comma

In regular expressions you can use original (find) string in replace - substitution. String you want to use must be enclosed by brackets.
(.*?\,.*?\,.*?\,.*?\,.*?\,)
This is the first part, followed by content of 6th column
(.*?)\,
Whan you replace, you use $n, where n means order number of search part. What I did is return whole first part as $1 without changes in replace, enclose content of the 6th column with "" and add comma:
$1"$2",

-----------------------------------------------------------
You can exchange parts in replace. E.g. you have german date format (dd.mm.yyyy):
12.3.2014
you can change it to US format (yyyy/mm/dd):

search: (\d+)\.(\d+)\.(\d+)
replace: $3/$2/$1

Options: Reply | Quote | Up ^


#10 Re: Replace the first occurence of the text

Posted by: pspad | Date: 2014-12-15 20:38 | IP: IP Logged

zales33:
This is an amazing tool that I wish I had more knowledge about. Might have to do some Googling or go to the librarysmiling smiley

Maybe you can try open PSPad help and find regular expressions in index. There is many examples and explanation.

Options: Reply | Quote | Up ^


Goto Page: 1 2 Next





Editor PSPad - freeware editor, © 2001 - 2024 Jan Fiala, Hosted by Webhosting TOJEONO.CZ, design by WebDesign PAY & SOFT, code Petr Dvořák, Privacy policy and GDPR