You are not logged in.

#1 2008-10-29 19:20:43

corrupt
Member
Registered: 2007-06-25
Posts: 74

grep: how to match one letter in a string

How to find a string with exaclty one occurence of some letter?
For example that regular expresion must match "3434w3456" and not match "642w567w4".

thanks

Offline

#2 2008-10-29 19:36:15

icetonic
Member
From: Germany
Registered: 2008-10-21
Posts: 104

Re: grep: how to match one letter in a string

I doubt that you can express what you want as a regular expression... So I doubt grep can do this...

But I'm quite sure that sed can...

Unfortunately the only sed command I know and I ever needed is the replace command s/x/y/ ...

Offline

#3 2008-10-29 19:56:01

Procyon
Member
Registered: 2008-05-07
Posts: 1,819

Re: grep: how to match one letter in a string

Regex can do it with ^ inside brackets which negates it.
grep '^[^w]*w[^w]*$'

Offline

#4 2008-10-29 20:00:11

corrupt
Member
Registered: 2007-06-25
Posts: 74

Re: grep: how to match one letter in a string

thanks, Procyon.

Offline

#5 2008-10-29 20:04:02

Ramses de Norre
Member
From: Leuven - Belgium
Registered: 2007-03-27
Posts: 1,289

Re: grep: how to match one letter in a string

icetonic wrote:

I doubt that you can express what you want as a regular expression... So I doubt grep can do this...

But I'm quite sure that sed can...

Both sed and grep use regular expressions so their are equally powerful, and since you can easily design a finite state automaton for the language the OP is trying to match, it is certainly a regular language.

Offline

Board footer

Powered by FluxBB