Fundamental question about find-replacement with RegEx

General questions about using TextPad

Moderators: AmigoJack, bbadmin, helios, Bob Hansen, MudGuard

Post Reply
Rosseiro
Posts: 14
Joined: Tue Mar 22, 2011 1:33 am

Fundamental question about find-replacement with RegEx

Post by Rosseiro »

Hi people!
Textpad is THE text tool. Awesomely fantastic.

So is the regex capability. But I have a problem. Before going through it, I must say I have a feeling this question is so naive and general that I wouldn't find if I searched.

So here we go:

I have this piece of text:

Code: Select all

Art. 353. Cabe à Câmara Legislativa a análise e a autorização preliminar para implantação de nova tecnologia no sistema operacional de transporte coletivo do Distrito Federal, ressalvados os projetos em andamento e os a eles relacionados.
Art. 354. O dia 20 de novembro será considerado, no calendário oficial do Distrito Federal, como o Dia da Consciência Negra.
Art. 355. O Poder Público, observado o disposto na Constituição Federal e na legislação pertinente, estimulará, apoiará e divulgará o cooperativismo e outras formas associativas.
Art. 356. Os integrantes dos conselhos criados por esta lei, indicados pelo Poder Público, terão seus nomes referendados pela Câmara Legislativa, ressalvados os membros natos.


As you can see, the important sections begin with "Art. ###", where ### is a number that can vary from 0 to 400.

Also, the "Arts." are so cluttered, with no blank line to separate them. So I called the find-replace dialog and put this in the find...

Code: Select all

Art. [0-400]


and it naturally found those "Art. 353", "Art. 354" and so on.

And, in the replace with field, I typed:

Code: Select all

\nArt. [0-400].


BUT, it won't work! It appears that the Regex are useful only in the FIND field, not in the REPLACE! The output will be

Code: Select all

Art. [0-400].. A lei disporá sobre a criação e regulamentação do Conselho de Defesa do Consumidor do Distrito Federal.

Art. [0-400].. A lei disporá sobre a criação e regulamentação do Conselho de Defesa dos Direitos da Criança e do Adolescente do Distrito Federal.

Art. [0-400].. Fica criado o Conselho do Idoso do Distrito Federal, encarregado de formular diretrizes, promover políticas para a terceira-idade e implementá-las, na forma da lei.


So, how could I do as follows: "have textpad find each instance of "Art. [0-400]", then replace it with a RETURN (new line, \n) followed by the instance found in case? for example, if it finds an "Art. 356", then replace with "\nArt. 356"? it will replace with "Art. [0-400]." instead.

Thanks for any help!
User avatar
MudGuard
Posts: 1295
Joined: Sun Mar 02, 2003 10:15 pm
Location: Munich, Germany
Contact:

Re: Fundamental question about find-replacement with RegEx

Post by MudGuard »

Rosseiro wrote:Textpad is THE text tool. Awesomely fantastic.
True.
Rosseiro wrote:So is the regex capability.
That's one of the weaker points ...

Rosseiro wrote:

Code: Select all

Art. 353. Cabe à Câmara Legislativa a análise e a autorização preliminar para implantação de nova tecnologia no sistema operacional de transporte coletivo do Distrito Federal, ressalvados os projetos em andamento e os a eles relacionados.
Art. 354. O dia 20 de novembro será considerado, no calendário oficial do Distrito Federal, como o Dia da Consciência Negra.
Art. 355. O Poder Público, observado o disposto na Constituição Federal e na legislação pertinente, estimulará, apoiará e divulgará o cooperativismo e outras formas associativas.
Art. 356. Os integrantes dos conselhos criados por esta lei, indicados pelo Poder Público, terão seus nomes referendados pela Câmara Legislativa, ressalvados os membros natos.


As you can see, the important sections begin with "Art. ###", where ### is a number that can vary from 0 to 400.

Also, the "Arts." are so cluttered, with no blank line to separate them. So I called the find-replace dialog and put this in the find...

Code: Select all

Art. [0-400]


does not do what you think it does.
It finds Art. followed by ONE digit which might be 0 to 4 or 0 or 0.

What you really want is

Code: Select all

Art. [0-4]?[0-9]?[0-9]
That means: Art. followed by 0 or 1 digits from the range of 0 to 4, 0 or 1 digits from range 0 to 9, and 1 digit from range 0 to 9.

To add a newline before that, you want to replace that with

Code: Select all

\n&
The & will be replaced by the String that was found.

Rosseiro wrote:and it naturally found those "Art. 353", "Art. 354" and so on.
No, it found "Art. 3", "Art. 3" and so on.

Rosseiro wrote:And, in the replace with field, I typed:

Code: Select all

\nArt. [0-400].


BUT, it won't work![/code]

It works perfectly. Just differently from what you want ...
Rosseiro wrote: It appears that the Regex are useful only in the FIND field, not in the REPLACE!
Of course not. You need a replacement expression there.

If you want to remember PARTS of the found string, the parts must be put in () in the search expression.
In the replacement, \1 stands for the first () in the find expression, \2 for the second (), \3 for the third () and so on.
(The number is taken from the opening (, as () might contain () ...)

But in your case, you want to put the complete found string into the replacement, therefore no need for ().
User avatar
SteveH
Posts: 327
Joined: Thu Apr 03, 2003 11:37 am
Location: Edinburgh, Scotland
Contact:

Post by SteveH »

MudGuard's reply shows how to achieve what you set out to do here but it may be that all you are needing to do is put another line feed at the end of each line.

In which case you can search for '$' (the end of line) and use the replacement expression '\n' to add an extra line.

Hope this helps.
Running TextPad 5.4 on Windows XP SP3 and on OS X 10.7 under VMWare or Crossover.
Rosseiro
Posts: 14
Joined: Tue Mar 22, 2011 1:33 am

Post by Rosseiro »

Gentlemen, I don't know how to thank you.

Perfect! Now I begin to see the power of regex in Textpad! :D
Post Reply