unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* Another replace regexp with list question
@ 2013-06-10  1:03 Leandro Marcolino
  2013-06-10 10:30 ` Peter Dyballa
  0 siblings, 1 reply; 8+ messages in thread
From: Leandro Marcolino @ 2013-06-10  1:03 UTC (permalink / raw)
  To: help-gnu-emacs

Hello, everyone!..

This time I am trying to find occurrences of:

"<span style='color:white'>&#31169;&#31169;&#31169;&#31169;</span>", but
the number of &#31169; can change from 1 to many.

And I want to replace each &#31169; of the list to a white space (besides
removing the <span> clause). So, if there were x "&#31169;", I need x " ".
Is it possible to do this using replace-regexp?.. Of course I can't just
look for "&#31169;" and replace to " ", because I have occurrences of
"&#31169;" that are not inside the group "<span
style='color:white'></span>".

I tried something like "<span style='color:white'>\(\(&#31169;\)+\)</span>
-> \,(cond (\1 " ")))", but then I lose the information of how many
"&#31169;" were there, and everything changes to a single " "....

Thanks for your help!.. :)

Regards,
Leandro


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Another replace regexp with list question
       [not found] <mailman.1310.1370826222.22516.help-gnu-emacs@gnu.org>
@ 2013-06-10  5:10 ` Barry Margolin
  2013-06-10  7:50   ` Leandro Marcolino
  2013-11-05  0:21   ` WJ
  2013-06-11 18:23 ` Sean McAfee
  1 sibling, 2 replies; 8+ messages in thread
From: Barry Margolin @ 2013-06-10  5:10 UTC (permalink / raw)
  To: help-gnu-emacs

In article <mailman.1310.1370826222.22516.help-gnu-emacs@gnu.org>,
 Leandro Marcolino <leandromarcolino@gmail.com> wrote:

> Hello, everyone!..
> 
> This time I am trying to find occurrences of:
> 
> "<span style='color:white'>&#31169;&#31169;&#31169;&#31169;</span>", but
> the number of &#31169; can change from 1 to many.
> 
> And I want to replace each &#31169; of the list to a white space (besides
> removing the <span> clause). So, if there were x "&#31169;", I need x " ".
> Is it possible to do this using replace-regexp?.. Of course I can't just
> look for "&#31169;" and replace to " ", because I have occurrences of
> "&#31169;" that are not inside the group "<span
> style='color:white'></span>".
> 
> I tried something like "<span style='color:white'>\(\(&#31169;\)+\)</span>
> -> \,(cond (\1 " ")))", but then I lose the information of how many
> "&#31169;" were there, and everything changes to a single " "....
> 
> Thanks for your help!.. :)
> 
> Regards,
> Leandro

Regular expressions can't count.

I would do it with a keyboard macro. Search for the span, mark it as a 
region, narrow to region, use replace-string to replace each &#31169 
with space, widen.

-- 
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Another replace regexp with list question
  2013-06-10  5:10 ` Another replace regexp with list question Barry Margolin
@ 2013-06-10  7:50   ` Leandro Marcolino
  2013-06-10 13:57     ` Drew Adams
  2013-11-05  0:21   ` WJ
  1 sibling, 1 reply; 8+ messages in thread
From: Leandro Marcolino @ 2013-06-10  7:50 UTC (permalink / raw)
  To: help-gnu-emacs

Oh, I see.... I didn't know about the "narrow" and "widen" commands before.
They are very useful! :) Thank you very much!..

Leandro


On Sun, Jun 9, 2013 at 10:10 PM, Barry Margolin <barmar@alum.mit.edu> wrote:

> In article <mailman.1310.1370826222.22516.help-gnu-emacs@gnu.org>,
>  Leandro Marcolino <leandromarcolino@gmail.com> wrote:
>
> > Hello, everyone!..
> >
> > This time I am trying to find occurrences of:
> >
> > "<span style='color:white'>&#31169;&#31169;&#31169;&#31169;</span>", but
> > the number of &#31169; can change from 1 to many.
> >
> > And I want to replace each &#31169; of the list to a white space (besides
> > removing the <span> clause). So, if there were x "&#31169;", I need x "
> ".
> > Is it possible to do this using replace-regexp?.. Of course I can't just
> > look for "&#31169;" and replace to " ", because I have occurrences of
> > "&#31169;" that are not inside the group "<span
> > style='color:white'></span>".
> >
> > I tried something like "<span
> style='color:white'>\(\(&#31169;\)+\)</span>
> > -> \,(cond (\1 " ")))", but then I lose the information of how many
> > "&#31169;" were there, and everything changes to a single " "....
> >
> > Thanks for your help!.. :)
> >
> > Regards,
> > Leandro
>
> Regular expressions can't count.
>
> I would do it with a keyboard macro. Search for the span, mark it as a
> region, narrow to region, use replace-string to replace each &#31169
> with space, widen.
>
> --
> Barry Margolin, barmar@alum.mit.edu
> Arlington, MA
> *** PLEASE post questions in newsgroups, not directly to me ***
>


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Another replace regexp with list question
  2013-06-10  1:03 Leandro Marcolino
@ 2013-06-10 10:30 ` Peter Dyballa
  0 siblings, 0 replies; 8+ messages in thread
From: Peter Dyballa @ 2013-06-10 10:30 UTC (permalink / raw)
  To: Leandro Marcolino; +Cc: help-gnu-emacs


Am 10.06.2013 um 03:03 schrieb Leandro Marcolino:

> This time I am trying to find occurrences of:
> 
> "<span style='color:white'>&#31169;&#31169;&#31169;&#31169;</span>", but
> the number of &#31169; can change from 1 to many.

	(replace-regexp "\\(<span style='color:white'>[&#3169;]+\\)&#31169;\\( *</span>\\)" "\\1 \\2")

In N-1 steps convert the last occurrence of "&#31169;" in a series of "&#31169;" to SPACE and bear in mind that the next time your regexp would need to have a SPACE before the final </span>.

The Nth step would be:

	(replace-regexp "\\(<span style='color:white'>\\)&#31169;\\( *</span>\\)" "\\1 \\2")

to convert the last left "&#31169;" to a SPACE.

--
Greetings

  Pete

"A TRUE Klingon warrior does not comment his code."




^ permalink raw reply	[flat|nested] 8+ messages in thread

* RE: Another replace regexp with list question
  2013-06-10  7:50   ` Leandro Marcolino
@ 2013-06-10 13:57     ` Drew Adams
  0 siblings, 0 replies; 8+ messages in thread
From: Drew Adams @ 2013-06-10 13:57 UTC (permalink / raw)
  To: Leandro Marcolino, help-gnu-emacs

> Oh, I see.... I didn't know about the "narrow" and "widen" commands before.
> They are very useful! :) Thank you very much!..

And you can have multiple narrowings in the same buffer, choosing
any of them anytime:

Description:
http://www.emacswiki.org/emacs/MultipleNarrowings

Code:
http://www.emacswiki.org/emacs-en/download/wide-n.el



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Another replace regexp with list question
       [not found] <mailman.1310.1370826222.22516.help-gnu-emacs@gnu.org>
  2013-06-10  5:10 ` Another replace regexp with list question Barry Margolin
@ 2013-06-11 18:23 ` Sean McAfee
  2013-06-11 22:13   ` Leandro Marcolino
  1 sibling, 1 reply; 8+ messages in thread
From: Sean McAfee @ 2013-06-11 18:23 UTC (permalink / raw)
  To: help-gnu-emacs

Leandro Marcolino <leandromarcolino@gmail.com> writes:
> This time I am trying to find occurrences of:
>
> "<span style='color:white'>&#31169;&#31169;&#31169;&#31169;</span>", but
> the number of &#31169; can change from 1 to many.
>
> And I want to replace each &#31169; of the list to a white space (besides
> removing the <span> clause). So, if there were x "&#31169;", I need x " ".

Replace: \(<span style='color:white'>\)\(\(?:&#31169;\)+\)\(</span>\)
With:    \1\,(make-string (/ (length \2) 8) ? )\3


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Another replace regexp with list question
  2013-06-11 18:23 ` Sean McAfee
@ 2013-06-11 22:13   ` Leandro Marcolino
  0 siblings, 0 replies; 8+ messages in thread
From: Leandro Marcolino @ 2013-06-11 22:13 UTC (permalink / raw)
  To: help-gnu-emacs

Oh, thanks!... I already solved the problem using keyboard macros, as Barry
suggested, but it is always good to learn!.. :) I will try out the multiple
narrowings and the replace-regexp suggestions. Thanks!..

Leandro


On Tue, Jun 11, 2013 at 11:23 AM, Sean McAfee <eefacm@gmail.com> wrote:

> Leandro Marcolino <leandromarcolino@gmail.com> writes:
> > This time I am trying to find occurrences of:
> >
> > "<span style='color:white'>&#31169;&#31169;&#31169;&#31169;</span>", but
> > the number of &#31169; can change from 1 to many.
> >
> > And I want to replace each &#31169; of the list to a white space (besides
> > removing the <span> clause). So, if there were x "&#31169;", I need x "
> ".
>
> Replace: \(<span style='color:white'>\)\(\(?:&#31169;\)+\)\(</span>\)
> With:    \1\,(make-string (/ (length \2) 8) ? )\3
>


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Another replace regexp with list question
  2013-06-10  5:10 ` Another replace regexp with list question Barry Margolin
  2013-06-10  7:50   ` Leandro Marcolino
@ 2013-11-05  0:21   ` WJ
  1 sibling, 0 replies; 8+ messages in thread
From: WJ @ 2013-11-05  0:21 UTC (permalink / raw)
  To: help-gnu-emacs

Barry Margolin wrote:

> In article <mailman.1310.1370826222.22516.help-gnu-emacs@gnu.org>,
>  Leandro Marcolino <leandromarcolino@gmail.com> wrote:
> 
> > Hello, everyone!..
> > 
> > This time I am trying to find occurrences of:
> > 
> > "<span style='color:white'>&#31169;&#31169;&#31169;&#31169;</span>", but
> > the number of &#31169; can change from 1 to many.
> > 
> > And I want to replace each &#31169; of the list to a white space (besides
> > removing the <span> clause). So, if there were x "&#31169;", I need x " ".
> > Is it possible to do this using replace-regexp?.. Of course I can't just
> > look for "&#31169;" and replace to " ", because I have occurrences of
> > "&#31169;" that are not inside the group "<span
> > style='color:white'></span>".
> > 
> > I tried something like "<span style='color:white'>\(\(&#31169;\)+\)</span>
> > -> \,(cond (\1 " ")))", but then I lose the information of how many
> > "&#31169;" were there, and everything changes to a single " "....
> > 
> > Thanks for your help!.. :)
> > 
> > Regards,
> > Leandro
> 
> Regular expressions can't count.
> 
> I would do it with a keyboard macro. Search for the span, mark it as a 
> region, narrow to region, use replace-string to replace each &#31169 
> with space, widen.

(replace-regexp-in-string
  "<span>\\(&#31169;\\)+</span>"
  (lambda (s)
    (make-string
      (length (replace-regexp-in-string "[^&]" "" s))
      ? ))
  "&#31169; hi<span>&#31169;&#31169;</span>there &#31169;")

  ===>

"&#31169; hi  there &#31169;"


^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2013-11-05  0:21 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [not found] <mailman.1310.1370826222.22516.help-gnu-emacs@gnu.org>
2013-06-10  5:10 ` Another replace regexp with list question Barry Margolin
2013-06-10  7:50   ` Leandro Marcolino
2013-06-10 13:57     ` Drew Adams
2013-11-05  0:21   ` WJ
2013-06-11 18:23 ` Sean McAfee
2013-06-11 22:13   ` Leandro Marcolino
2013-06-10  1:03 Leandro Marcolino
2013-06-10 10:30 ` Peter Dyballa

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).