Home > other >  RegEx for starting with certain character sets, a space, and then certain years
RegEx for starting with certain character sets, a space, and then certain years

Time:11-22

Needing help for a RegEx (using Oracle REGEXP_LIKE) to identify strings that:

  1. Start with any of the following:
  • P
  • C
  • (P)
  • (C)
  • ©
  1. are then followed by a space
  2. are then followed by any 4-digit numbers, where the first 2 digits in the number are 19 or 20.
  3. are then followed by a space
  4. are then followed by any other text

Sample matches:

(P) 2004 XYZ Company P 2018 This is some random text (C) 1994 More Random Text

Sample non-matches:

(P) XYZ Company P1976,Just Wow (C) 1856 Too Late For Gold

I've started with

^[PC©]\s 

but, as a beginner, am stumped with how to handle the (P) and (C) cases, much less the complexities that follow with the year values.

CodePudding user response:

You may use the following pattern:

SELECT *
FROM yourTable
WHERE REGEXP_LIKE(text, '^([CP©]|\([CP]\)) (19|20)[0-9]{2} .*');

CodePudding user response:

A dirty solution is to just use alternation. For example, (abc|def) will match abc or def.

For your case: ^(\(P\)|\(C\)|P|C|©) (19|20)\d{2} .*

  • Related