PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Forums Hébergement > Forum Serveur - Sécurité et techniques > comp.unix.shell > sed: How do you match a string containing square brackets AND hyphens?
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
comp.unix.shell Using and programming the Unix shell.

sed: How do you match a string containing square brackets AND hyphens?

Réponse
 
LinkBack Outils de la discussion
Vieux 23/08/2006, 16h00   #1
Dave S.
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut sed: How do you match a string containing square brackets AND hyphens?

The streaming editor (sed) allows us to use the metacharacters "[" and
"]" to match any one of a number of characters. According to the
Pattern Matching chapter in O'Reilly's book Unix in a Nutshell by
Daniel Gilly, "A hyphen or close bracket (]) as the first character is
treated as a member of the list."

That's great, but what if I need to match a string that may contain
brackets AND hypens?

I need to match a C++ variable name or constant that may include any of
these characters:
A-Z
a-z
0-9
- (hypen)
_ (underscore)
> (as in ->)

[ and ]
.. (dot)

Here is what I'd like to be able to do:
s/something\([A-Za-z0-9_->\[\]\.]*\)the_rest/something_else \1
the_rest/

That does not work.

What can I do?

Why is simply escaping the [ and ] characters in the list not good
enough?

  Réponse avec citation
Vieux 23/08/2006, 16h27   #2
Jon LaBadie
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: sed: How do you match a string containing square brackets ANDhyphens?

Dave S. wrote:
> The streaming editor (sed) allows us to use the metacharacters "[" and
> "]" to match any one of a number of characters. According to the
> Pattern Matching chapter in O'Reilly's book Unix in a Nutshell by
> Daniel Gilly, "A hyphen or close bracket (]) as the first character is
> treated as a member of the list."
>
> That's great, but what if I need to match a string that may contain
> brackets AND hypens?
>
> I need to match a C++ variable name or constant that may include any of
> these characters:
> A-Z
> a-z
> 0-9
> - (hypen)
> _ (underscore)
>> (as in ->)

> [ and ]
> . (dot)
>
> Here is what I'd like to be able to do:
> s/something\([A-Za-z0-9_->\[\]\.]*\)the_rest/something_else \1
> the_rest/
>
> That does not work.
>
> What can I do?
>
> Why is simply escaping the [ and ] characters in the list not good
> enough?
>


If the backslash, '\' were an escape character inside brackets
it might work. But it is not and escape character in that context.

The O'Reilly book also said, or should have said, a hyphen as the
first or the last character is treated as a member of the list.
So you could do []xyz-] to make a list matching 5 characters.
  Réponse avec citation
Vieux 23/08/2006, 16h30   #3
Xicheng Jia
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: sed: How do you match a string containing square brackets AND hyphens?

Dave S. wrote:
> The streaming editor (sed) allows us to use the metacharacters "[" and
> "]" to match any one of a number of characters. According to the
> Pattern Matching chapter in O'Reilly's book Unix in a Nutshell by
> Daniel Gilly, "A hyphen or close bracket (]) as the first character is
> treated as a member of the list."
>
> That's great, but what if I need to match a string that may contain
> brackets AND hypens?
>
> I need to match a C++ variable name or constant that may include any of
> these characters:
> A-Z
> a-z
> 0-9
> - (hypen)
> _ (underscore)
> > (as in ->)

> [ and ]
> . (dot)
>
> Here is what I'd like to be able to do:
> s/something\([A-Za-z0-9_->\[\]\.]*\)the_rest/something_else \1
> the_rest/
>
> That does not work.
>
> What can I do?


try this: []A-Z0-9_.>a-z[-]

> Why is simply escaping the [ and ] characters in the list not good
> enough?


This looks quite different from Perl's regex where you can use
backslashes to escape them and then put them in the middle of the
class-set.

--
XC

  Réponse avec citation
Vieux 23/08/2006, 16h38   #4
Dave S.
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: sed: How do you match a string containing square brackets AND hyphens?

Jon,

No, the O'Reilly book didn't say anything about making a hyphen the
last character to escape its meaning. I'll try that.

But that still leaves the opening square bracket to be dealt with. What
about it?

Thank you.


Jon LaBadie wrote:

> If the backslash, '\' were an escape character inside brackets
> it might work. But it is not and escape character in that context.
>
> The O'Reilly book also said, or should have said, a hyphen as the
> first or the last character is treated as a member of the list.
> So you could do []xyz-] to make a list matching 5 characters.


  Réponse avec citation
Vieux 23/08/2006, 16h38   #5
Stephane Chazelas
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: sed: How do you match a string containing square brackets AND hyphens?

On 23 Aug 2006 08:00:13 -0700, Dave S. wrote:
> The streaming editor (sed) allows us to use the metacharacters "[" and
> "]" to match any one of a number of characters. According to the
> Pattern Matching chapter in O'Reilly's book Unix in a Nutshell by
> Daniel Gilly, "A hyphen or close bracket (]) as the first character is
> treated as a member of the list."

[...]

$ echo '[]-' | sed 's/[][-]/+/g'
+++

See also: sed 'y/[]-/+++/'

--
Stephane
  Réponse avec citation
Vieux 23/08/2006, 16h46   #6
Jon LaBadie
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: sed: How do you match a string containing square brackets ANDhyphens?

Dave S. wrote:
> Jon,
>
> No, the O'Reilly book didn't say anything about making a hyphen the
> last character to escape its meaning. I'll try that.
>
> But that still leaves the opening square bracket to be dealt with. What
> about it?
>
> Thank you.
>
>
> Jon LaBadie wrote:
>
>> If the backslash, '\' were an escape character inside brackets
>> it might work. But it is not and escape character in that context.
>>
>> The O'Reilly book also said, or should have said, a hyphen as the
>> first or the last character is treated as a member of the list.
>> So you could do []xyz-] to make a list matching 5 characters.

>


The opening bracket can be anywhere in the list.
  Réponse avec citation
Vieux 23/08/2006, 16h52   #7
Dave S.
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: sed: How do you match a string containing square brackets AND hyphens?

Okay, I see from Stephane's example that the left square bracket ([)
can be included in the list like any other character.

So what I have to do is:
1. Make the closing square bracket the first character in the list.
2. Make the hyphen the last character in the list.
3. Put the opening square bracket somewhere in the middle.

THANKS EVERYONE!


Stephane Chazelas wrote:
> $ echo '[]-' | sed 's/[][-]/+/g'


  Réponse avec citation
Vieux 23/08/2006, 17h43   #8
Chris Mattern
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: sed: How do you match a string containing square brackets ANDhyphens?

Dave S. wrote:
> Jon LaBadie wrote:
>>If the backslash, '\' were an escape character inside brackets
>>it might work. But it is not and escape character in that context.
>>
>>The O'Reilly book also said, or should have said, a hyphen as the
>>first or the last character is treated as a member of the list.
>>So you could do []xyz-] to make a list matching 5 characters.

>
>
> Jon,
>
> No, the O'Reilly book didn't say anything about making a hyphen the
> last character to escape its meaning. I'll try that.
>
> But that still leaves the opening square bracket to be dealt with. What
> about it?
>
> Thank you.
>
>


Top-posting fixed. Please don't do that.

What makes you think the opening square bracket needs to be
dealt with at all? Just put it in. []xyz[-], for example.


Chris Mattern
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 19h27.


Édité par : vBulletin® version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,12692 seconds with 16 queries