|
|
|
|
||||||
| comp.unix.shell Using and programming the Unix shell. |
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#1 |
|
Messages: n/a
Hébergeur: |
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? |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#7 |
|
Messages: n/a
Hébergeur: |
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' |
|
|
|
#8 |
|
Messages: n/a
Hébergeur: |
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 |
|
![]() |
| Outils de la discussion | |
|
|