0

I want a regular expression to match all strings like 'T' OR 'T-Th' but NOT values containing only 'Th'

my current regexp is this one:

REGEXP = '[T-Th]' 

but it matches all strings 'T' , 'T-Th' and 'Th' which 'Th' is not desirable

5
  • 1
    This isn't exactly clear. Can you post specific complete examples of strings that should match and examples of those which should be rejected? Commented Apr 28, 2014 at 20:15
  • [T-Th] is the same as [Th] (which matches T or h, not "Th"). Commented Apr 28, 2014 at 20:15
  • i would like to match strings such as 'T' , 'T-Th' but not strings containing only the value 'Th' Commented Apr 28, 2014 at 20:16
  • @ather0s Provide samples in the question. Commented Apr 28, 2014 at 20:16
  • You mean literally T or T-Th, rather than words beginning with or containing T or Th as the [] character kind of implies? Commented Apr 28, 2014 at 20:18

3 Answers 3

1
REGEXP = '(T-Th|T[^h])' 

We either match T-Th literally, OR match T that is not followed by anything but an h. In other words: T, T-Th, but not Th.

Regex101

Sign up to request clarification or add additional context in comments.

Comments

1

If I have the logic correct, you want a 'T' in the string and you want a 'T' not followed by 'h':

where col like '%T%' and
      replace(col, 'Th', '') like '%T%'

Comments

0

The - character between brackets is special for regex. It must be escaped when the use is literal. A good alternative for your problem could be this:

REGEXP = '(T-Th|T(?!h))'

5 Comments

The - is only special inside a character class.
Aside from the escaping not needed, I think this might be what the OP is asking for...
@MichaelBerkowski Wrong information is still wrong and should be corrected before being accepted. Otherwise you ingest poison with an otherwise good bottle of wine.
@user2864740 I don't disagree. My comment was more pointing the fact that the question is still really unclear.
Actually @user2864740 was right, but with backslash works also. Corrected!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.