shell-script-pt
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RES: [shell-script] Pegar tudo entre ' '


From: Julio Cezar Neves - DISB.O
Subject: RES: [shell-script] Pegar tudo entre ' '
Date: Thu, 7 Mar 2002 10:04:13 -0300

Fala Aurélio, *meu guru de regexp*,
entendi legal a sua solução fazendo agrupamentos com parênteses para
posterior substituição por \1 e \2. Pra te falar a verdade, foi a 1a.
solução que me ocorreu, mas achei que a minha era + simples. Quebrei a cara
pq não me ocorreu que podia ter mais de uma palavra entre plics. Faltou
experiencia (malandragem?) em regexp.

Mudando de assunto, me caiu no colo um big sistema escrito em perl e estou
estudando a linguagem, que como vc sabe usa regexp abundantemente (oops!).
Vi uma construção muito interesante, parecida com o caso da lista que
estamos discutindo, que não entendi legal a solução dada. Abaixo o texto ao
qual me refiro:

==========================================================================

Glad you followed that. Now, pay even closer attention ! Concentrate fully
on the task at hand ! This should be straightforward now: 

$_='HTML <I>munging</I> time !.';

/<I>(.*)<\/I>/i;

print "Found it ! $1\n";

Pretty much the same as the above, except the parens are moved so we return
what's only inside the tags, not including the tags themselves. Also note
how / is escaped like so; \/ otherwise Perl thinks that's the end of the
regex. 
Now, suppose we change $_ to : 

$_='HTML <I>munging</I> time is here <I>again</I> !.';


and run it again. Interesting effect, eh ? This is known as Greedy Matching.
What happens is that when Perl finds the initial match, that is <I> it jumps
right to the end of the string and works back from there to find a match, so
the longest string matches. This is fine unless you want the shortest
string. And there is a solution: 
/<I>(.*?)<\/I>/i;

Just add a question mark and Perl does stingy matching. No nationalistic
jokes. I have Dutch and Scottish friends I don't want to offend. 

==========================================================================

Tenho liberdade contigo para te mandar esta dúvida em pvt, mas ousei colocar
este trecho de tutorial na lista shell script, pq ele não tem nada de perl.
É regexp puro, e como tal serve para todos os "shelleiros"! 

Agora a minha dúvida: porque este último parágrafo? Não entendi legal pq o ?
limita a pesquisa. Até entendi a piada +/- sutil e sem graça sobre a avareza
do escocês e do alemão, mas a do ? não.

Agradeço a sua dica. [ ]s do amigo,
Julio


-----Mensagem original-----
De: # aurelio marinho jargas [mailto:address@hidden]
Enviada em: quarta-feira, 6 de março de 2002 14:14
Para: address@hidden
Assunto: Re: [shell-script] Pegar tudo entre ' '


grande mestre julio,

@ 6/3, Julio Cezar Neves - DISB.O:
> não sou especialista em expressões regulares e tenho um medo mortal do .*
> (ou asterisco guloso, como define o Aurélio Marinho Jargas), mas testei o
> sed abaixo e funcionou.
>
> sed "s/'.*'/'seilaoque'/g"
>
> Olha o teste:
>
> Prompt> cat a
> ssssss'aaaaa'xxxxx
> Prompt> sed "s/'.*'/'seilaoque'/g" a
> ssssss'seilaoque'xxxxx

esse seu sed tá beleza!
se a entrada que ele especificou sempre tiver apenas UMA
ocorrência de string entre aspas, não vai falhar.

agora se também tiver algo do tipo

        blabla balb 'blabla' blabla 'blabla'

aí o curinga .* vai casar:

        'blabla' blabla 'blabla'

tudo de uma vez &:)


mas a variante não gulosa é fácil, basta trocar o . por algo mais
específico como "qualquer coisa menos uma aspa"


guloso: sed "s/'.*'/'seilaoque'/g"
tímido: sed "s/'[^']*'/'seilaoque'/g"




> De: Tiago N. Sampaio [mailto:address@hidden]
> Ola pessoal preciso alterar uma string que vem entre ' '
>
> a string que vem é algo assim
> seila seila seila seil 'seila2'


-- 
http://verde666.org s/click/^a]/;^[:wq


Sair da lista: address@hidden
Banco Dados:   http://www.egroups.com/group/shell-script 

Seu uso do Yahoo! Grupos é sujeito às regras descritas em:
http://br.yahoo.com/info/utos.html 



reply via email to

[Prev in Thread] Current Thread [Next in Thread]