Home > Mobile >  Writing a Regular expression to capture 3 lines from big text
Writing a Regular expression to capture 3 lines from big text

Time:01-03

I have an assignment which is to write only re expression to capture these three lines

YOUTH WRITING WORKSHOPS ACADEMIC SUPPORT
YOUTH WRESTLING CLUB SUPPORTED YOUTH WRESTLING
YOUTH WORK THERAPY PROGRAMS

from very big text column in a postgresql table.

I've tried a lot of code and can get them with help of postgre functions, but the assignment need them in the same order with only re.

here is the start query.

SELECT purpose FROM taxdata WHERE purpose ~ '^[A-Z]' ORDER BY purpose DESC LIMIT 3;

I tried these expressions but they partially work cause I think it only matches the first few words.

^[A-Z][A-Z]... W[A-Z][A-Z]
^YO... W.{,39}[ORT|ING|AMS]$

and others but always fail honestly, I don't like or use re that much, so help please.

CodePudding user response:

Why not => /^YOUTH WRITING WORKSHOPS ACADEMIC SUPPORT YOUTH WRESTLING CLUB SUPPORTED YOUTH WRESTLING YOUTH WORK THERAPY PROGRAMS$/gm

CodePudding user response:

If you want to use the quantifier and match one of the 3 character groups at the end, you can write the quantifier like {0,39} and use an alternation at the end instead of a character class like (?:ORT|ING|AMS)$

SELECT purpose FROM taxdata WHERE purpose ~ '^YO... W.{0,39}(?:ORT|ING|AMS)$' ORDER BY purpose DESC LIMIT 3;

See a dbfiddle

  • Related