Space after first word in string

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
LLSmith
Forum Newbie
Posts: 4
Joined: Tue May 04, 2010 7:37 am

Space after first word in string

Post 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
mikosiko
Forum Regular
Posts: 757
Joined: Wed Jan 13, 2010 7:22 pm

Re: Space after first word in string

Post by mikosiko »

show us the code that you are done so far and tell us which are your doubts

Miko
LLSmith
Forum Newbie
Posts: 4
Joined: Tue May 04, 2010 7:37 am

Re: Space after first word in string

Post by LLSmith »

/^(\s*\S+) [ ]/m,$1
mikosiko
Forum Regular
Posts: 757
Joined: Wed Jan 13, 2010 7:22 pm

Re: Space after first word in string

Post by mikosiko »

preg_replace('/\s/', ',', $yourline, 1)
LLSmith
Forum Newbie
Posts: 4
Joined: Tue May 04, 2010 7:37 am

Re: Space after first word in string

Post 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
mikosiko
Forum Regular
Posts: 757
Joined: Wed Jan 13, 2010 7:22 pm

Re: Space after first word in string

Post by mikosiko »

LLSmith wrote:I'm trying to use this code inside a text editor while allows search and replace with regex
:D
no wonder :)

which text editor are you using?...

try this:
Regex: ([A-Z]+) (.*)
Replace with: \1,\2
LLSmith
Forum Newbie
Posts: 4
Joined: Tue May 04, 2010 7:37 am

Re: Space after first word in string

Post by LLSmith »

UltraEdit Studio 09
User avatar
ridgerunner
Forum Contributor
Posts: 214
Joined: Sun Jul 05, 2009 10:39 pm
Location: SLC, UT

Re: Space after first word in string

Post 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.
Post Reply