George
George

Reputation: 157

Re.search not matching on same string

I am using re.search(), and I'm trying to match a line from a file, however, when I'm expected a match it's not finding it. I've used the code before but now it's not working now and I am puzzled as to why.

I've tried using .strip(), still no luck

if re.search("/",line):
    new_line = line.split("/")
    for sub_design in seq_dict:
        match = re.search(sub_design, new_line[0])
        if match:
            seq_dict[match.group()] += 1
        else:
            print("sub design is")
            print(sub_design)
            print ("new line is")
            print(new_line[0]) 

The sub design contains 5 different strings, pulled from somewhere else.

I get these kind of prints :

The line variable would have the following string that it's read from a file:

hi_george_top[0].u_are_here/hi_george_sub/   something      great  

sub design is

hi_george_top[0].u_are_here

new line is

hi_george_top[0].u_are_here

Upvotes: 0

Views: 132

Answers (1)

Corentin Limier
Corentin Limier

Reputation: 5006

I have the feeling that regex is not a solution here, I suggested in comment using the in statement instead.

line = "hi_george_top[0].u_are_here/hi_george_sub/   something      great"
seq_dict = {
    'bzhejf': 0,
    'eznjzfe' : 0,
    'zbjez' : 0,
    'hi_george_top[0]': 0
}
if '/' in line:
    new_line = line.split("/")
    for sub_design in seq_dict:
        if sub_design in new_line[0]:
            seq_dict[sub_design] += 1

print(seq_dict)

Displays :

{'bzhejf': 0, 'eznjzfe': 0, 'zbjez': 0, 'hi_george_top[0]': 1}

Upvotes: 1

Related Questions