1834089 Members
2268 Online
110063 Solutions
New Discussion

Reg Exp. Problem

 
Joshila
New Member

Reg Exp. Problem

I need to write a regular expression which will match a string value for N's and T's but not a combination like NTNTNTN

This expression is what i wrote

[N*T*]$

Acceptable values of string
N
NNNNN.... any number of N's
T
TTTTTT ... any number of T's
N N N N there can be spaces inbetween
T T T T ...
but NOT something NTNTNTN or TNTNTNT
but the expression above is matching this combination tooo ...

Can anyone help me solve this?

3 REPLIES 3
Elmar P. Kolkman
Honored Contributor

Re: Reg Exp. Problem

Solution I would use: {N+|T+}$
It depends on the program you use if this is a supported regex. And use + instead of *, because you don't want to match empty patterns.
Every problem has at least one solution. Only some solutions are harder to find.
Elmar P. Kolkman
Honored Contributor

Re: Reg Exp. Problem

For egrep:
egrep 'N+$|T+$'
Every problem has at least one solution. Only some solutions are harder to find.
Elmar P. Kolkman
Honored Contributor

Re: Reg Exp. Problem

Sorry, missed one thing...
Should be:
egrep '^N+$|^T+$'

Every problem has at least one solution. Only some solutions are harder to find.