2

I'm working with regular expression on python then I've the followings string that I need to parse some like

XCT_GRUPO_INVESTIGACION_F1.sql
XCT_GRUPO_INVESTIGACION_F2.sql
XCT_GRUPO_INVESTIGACION.sql
XCS_GRUPO_INVESTIGACION.sql

The I need to parse all the string that has ??T, but the string not must containt somthing like F1,F34,constrains and others

So I've the following pattern

([a-zA-Z][a-zA-Z][tT]_([a-zA-Z]).*.(sql|SQL)$)

[a-zA-Z][a-zA-Z][tT]_ = check the first and second value could be whatever but I need to be followed by t_ or T_

([a-zA-Z]).* = any value a-z and A-Z any times

(sql|SQL)$ = must be end with sql or SQL

I get something like

ICT_GRUPO_INVESTIGACION_F1.sql
ICT_GRUPO_INVESTIGACION_F2.sql
ICT_GRUPO_INVESTIGACION.sql

But this contains F1,F?,constrains and others

how can I say to the regular expression that in the expression ([a-zA-Z]).* no contains f1 | f? | others_expresion_that_Iwanna

1
  • 5
    Can you do this without regex? Regexes are usualy quite slow and it will often be faster and easier to use loops and in statements. You could easily use Pythons endswith method to find .sql files, and the in method to exclude files with the patterns you mentioned (e.g. F1, F34). Commented Apr 28, 2015 at 16:29

1 Answer 1

0

This regular expression should work:

([a-zA-Z][a-zA-Z][tT]_(?:(?!_F[0-9]).)*?\.(sql|SQL))

You may put any number of unwanted combinations here (?!_F[0-9]|other_expression|...)

There are following parts in the regular expression:

[a-zA-Z]         #match any letter
[a-zA-Z]         #match any letter
[tT]_            #match 't_' or 'T_'
(?:              #start non-capturing group
  (?!_F[0-9])    #negative lookahead, asserts that what immediately          
                 #follows the current position in the string is not _f[0-9]
  .              #match any single character
)*?              #end group, repeat it multiple times but as few as possible
\.               #match period character
(sql|SQL)        #match 'sql' or 'SQL'

You could find additional information here, here and here

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

Comments

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.