Need to match two patterns in each line

Need to match two patterns in each line

I have written a code (please see below) to read a text file and then search in each line a range of GW (in this sample it goes from GW-0 until GW-9, but in reality I will handle bigger ranges of thousands values) together with SNB.
To summarize, I want to count for a given range of GWs how many SNB values are present.
I tried different ways of modifying my regular expressions but still didn’t get the desired output.
Using the code below I should have 8 as an ouput , since 8 GWs do have SNB number and 2GWs don’t.
Can you please help me figure out what is wrong with the regular expressions I am using?
The sample from text file is as follows:

DEV             STATE RDM   TYPE  ADM  ABS   SNB           SNBST  LIST
GW-0          NEW                    H'0   321xxxxxx     NEW 
GW-1          BLOC  LEN         NC   H'0
GW-2          NEW                    H'40  321xxxxxx     NEW 
GW-3          NEW                    H'0   321xxxxxx     NEW 
GW-4          NEW                    H'0   321xxxxxx     NEW 
GW-5          NEW                    H'0   321xxxxxx     NEW 
GW-6          NEW                    H'0                
GW-7          NEW                    H'0   321xxxxxx     NEW 
GW-8          NEW                    H'0   321xxxxxx     NEW 
GW-9          NEW                    H'0   321xxxxxx     NEW 

And the part of the code is:

import os
import re

mylines = []
idles =[]

with open ("E:\Data\Projet situatis\Output_st.txt", "rt") as myfile:  
    for myline in myfile:             
        #Search all GWs for RG 0-9'^GW-[0-9] ',myline)'[321]*',myline)
        if GWUID_500_1 and SNB:
    print (nb_500)


GWUID_500_1 ='GW-[0-9]', myline)
SNB ='[0-9]{3}x+', myline)

Try those regexes, they will get your code to return 8 :slight_smile:


‘GW-[0-9]’ >> Match GW- and number from 0 to 9, if you want to expand this, just put a + after it.

‘[0-9]{3}x+’ >> exactly 3 occurences of digits, then at least 1 occur of x

Try to use this site to validate your regex:

GW UID Validation:

SNB Validation:

Encourage you to test your regexes over this site to check exactly what u was matching and why this was returning 10 instead of 8.

1 Like

Hi , thanks a lot for the feedback and for the very useful link!!

1 Like

Glad to help, have a nice code! :laughing: Feel free to ask if need.