A stupid question

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

A stupid question

Enrico Colombini
I have to change a whole word (s) with another word (sh) in a string
(line), using gsub, but I can't find a simple way to do it.

Suppose the first word (s) is "the"; I want to change every occurrence of
"the" but not to change the "the" in "theory", "atheist" or "lithe".

So far, the only way I found is this, but it seems a bit ridicolous:

  line = gsub(line, '(%W)'..s..'(%W)', '%1'..sh..'%2') -- in the middle
  line = gsub(line, '^'..s..'(%W)', sh..'%1') -- start of line
  line = gsub(line, '(%W)'..s..'$', '%1'..sh) -- end of line
  line = gsub(line, '^'..s..'$', sh) -- alone on the line

My problem comes from the lack of a "word boundary" magic character in
patterns (like "\b" in Perl). "%W" requires an existing character in that
position, so it doesn't seem to work at the beginning or at the end of a
string, and ^$ anchors can't be used in [character sets].

I suppose I am doing something wrong; is there a simpler way and I missed
something obvious?

  Enrico


Reply | Threaded
Open this post in threaded view
|

Re: A stupid question

Tomas Guisasola Gorham-2
> I have to change a whole word (s) with another word (sh) in a string
> (line), using gsub, but I can't find a simple way to do it.
> 
> Suppose the first word (s) is "the"; I want to change every occurrence of
> "the" but not to change the "the" in "theory", "atheist" or "lithe".

function change (src, find, rep)
	src = gsub (src, "(%w+)", function (word)
		if word == %find then
			return %rep
		else
			return word
		end
	end)
	return src
end

> print(change("the theory abcthedef", "the", "THE"))
THE theory abcthedef

	Is it what you wanted?

		Tomas


Reply | Threaded
Open this post in threaded view
|

Re: A stupid question

Roberto Ierusalimschy-5
In reply to this post by Enrico Colombini
On Fri, 11 Jan 2002, Enrico Colombini wrote:

> I have to change a whole word (s) with another word (sh) in a string
> (line), using gsub, but I can't find a simple way to do it.
>
> Suppose the first word (s) is "the"; I want to change every occurrence of
> "the" but not to change the "the" in "theory", "atheist" or "lithe".

Another solution is like this:

  line = ' '..line..' '
  line = gsub(line, '(%W)'..s..'(%W)', '%1'..sh..'%2') -- in the middle
  line = strsub(line, 2, -2)

-- Roberto


Reply | Threaded
Open this post in threaded view
|

Re: A stupid question

Enrico Colombini
In reply to this post by Enrico Colombini
>function change (src, find, rep)
>	src = gsub (src, "(%w+)", function (word)
>		if word == %find then
>			return %rep
>		else
>			return word
>		end
>	end)
>	return src
>end
>
>> print(change("the theory abcthedef", "the", "THE"))
>THE theory abcthedef
>
>	Is it what you wanted?

Yes, I think that will do the trick (and it doesn't look too costly in
terms of performance).

Thanks, I see that I have to learn to think more 'functionally'... and
study better the examples in the manual :-)

  Enrico


Reply | Threaded
Open this post in threaded view
|

Re: A stupid question

Enrico Colombini
In reply to this post by Enrico Colombini
>Another solution is like this:
>
>  line = ' '..line..' '
>  line = gsub(line, '(%W)'..s..'(%W)', '%1'..sh..'%2') -- in the middle
>  line = strsub(line, 2, -2)

Thanks... now I feel like a complete idiot :-)

  Enrico


Reply | Threaded
Open this post in threaded view
|

Re: A stupid question

Tomas Guisasola Gorham-2
> >Another solution is like this:
> >
> >  line = ' '..line..' '
> >  line = gsub(line, '(%W)'..s..'(%W)', '%1'..sh..'%2') -- in the middle
> >  line = strsub(line, 2, -2)
	In fact, you'll have to be carefull with punctuation.
If s == "a.b" you'll have to escape it before using in the
pattern:

s = gsub (s, "(%p)", "%%%1")

	So, "a.b" will be "corrected" to "a%.b".  This is required for
every solutions given (mine, Roberto's ...).

	Tomas


Reply | Threaded
Open this post in threaded view
|

Re: A stupid question

Enrico Colombini
In reply to this post by Enrico Colombini
>If s == "a.b" you'll have to escape it before using in the
>pattern:
>
>s = gsub (s, "(%p)", "%%%1")
>
>	So, "a.b" will be "corrected" to "a%.b".  This is required for
>every solutions given (mine, Roberto's ...).

Yes, I thought of that, but it never hurts to think twice :-)

  Enrico