Nestor Colt
Nestor Colt

Reputation: 377

python regex find any letter or word inside underscore

My strings can be:

string = 'thumbC_L_001_JOIN' 
string = 'L_thumbC_001_JOIN'

match = re.match(r'^(?:\b\w|_){}(?:\b\w|_)+'.format('L'), chain,flags=re.IGNORECASE)

    print(match)

I need to find the letter L in this case wherever it be between two underscores or at the beginning of the string with an underscore after it

Upvotes: 0

Views: 557

Answers (1)

The fourth bird
The fourth bird

Reputation: 163632

Maybe you could use lookaheads with an alternation to assert that the letter L is at the start of the string followed by an underscore or that the letter L is surrounded by an underscore:

^L(?=_)|(?<=_)L(?=_)

  • ^L(?=_) Match L and assert that what follows is an underscore
  • | Or
  • (?<=_)L(?=_) Assert that what is on the left is an underscore, match Land assert that what follows is an underscore

Upvotes: 1

Related Questions