Home > Enterprise >  Regular expression - check if its the pattern at the end of string
Regular expression - check if its the pattern at the end of string

Time:08-05

I have a list of strings like this:

something-12230789577

and I need to extract digits that end with a question mark symbol or NOTHING (which means the found pattern is at the end of the string) Match here should be: '12230789577' I wrote:

r'\d [?|/|]'

but it returns no results in this example. \s works for space symbol, but here I'm met with an empty symbol so \s is not needed. How can I add the empty symbol (end of string) to the regex condition?

CodePudding user response:

This might work:

re.search(r'\d [?]?', t)

where t is the text input [] checks for a character ? checks for 0 or 1 occurrence.

CodePudding user response:

Keeping ?|/ symbols optional(0 or 1).

import re
a='something-A12230789577'
b=re.search(r'\d [?|/]?',a)
b

  • Related