Page 1 of 1
Space after first word in string
Posted: Tue May 04, 2010 10:21 am
by LLSmith
I have approximately 4000 lines of text. The text string has spaces removed or trimmed from leading side and trailing side of string. I need to do the following:
Example string: This is an example of a text string with several spaces.
Discussion: About 4000 lines each with CRLF are contained in the text file. I need to look at each line, find the first word in the text, replace the space between the first and second word with a comma and then go to the next line. After the comma is inserted or replaced in the text, nothing remaining needs to be preformed on that line. The next line is to be addressed and the process repeated. Thanks for any help.
Larry
Re: Space after first word in string
Posted: Tue May 04, 2010 10:44 am
by mikosiko
show us the code that you are done so far and tell us which are your doubts
Miko
Re: Space after first word in string
Posted: Tue May 04, 2010 12:42 pm
by LLSmith
/^(\s*\S+) [ ]/m,$1
Re: Space after first word in string
Posted: Tue May 04, 2010 1:36 pm
by mikosiko
preg_replace('/\s/', ',', $yourline, 1)
Re: Space after first word in string
Posted: Tue May 04, 2010 2:00 pm
by LLSmith
Thanks for your help. I'm trying to use this code inside a text editor while allows search and replace with regex. I used the code you provided and also tried other versions of it. It will not allow me to use directly your code
preg_replace('/\s/', ',', $yourline, 1). I just need the expression to use in the search part of the text editor and of course in the replace portion I have placed the comma ',' The text engine only responds to pure regex expression. I hope I have explained this better this time. thanks
Re: Space after first word in string
Posted: Tue May 04, 2010 2:23 pm
by mikosiko
LLSmith wrote:I'm trying to use this code inside a text editor while allows search and replace with regex
no wonder
which text editor are you using?...
try this:
Regex: ([A-Z]+) (.*)
Replace with: \1,\2
Re: Space after first word in string
Posted: Tue May 04, 2010 3:22 pm
by LLSmith
UltraEdit Studio 09
Re: Space after first word in string
Posted: Wed May 05, 2010 9:13 am
by ridgerunner
Your first regex is almost correct. You need to select the "Perl" Regex style and do this:
[text]Find What:
^([-\w]+)[ ]
Replace With:
$1,[/text]
You need the ^ at the beginning to match words at the beginning of each line only. The above regex matches words having alpha-numerics and dash and underscores only.