Search method in re module of Python

ret=re.search (r"\ dudes)
print (ret.group ())
results do not meet expectations: 999


first of all, search is defined as follows:

:

so, the first match is the position before the first letter h, so you use group () to get the data and, of course, return an empty string


1. First of all, take a look at the regular basic metacharacters:

< table > < thead > < tr > < th > Code < / th > < th align= "center" > description < / th > < / tr > < / thead > < tbody > < tr > < td >. < / td > < td align= "center" > matches any single character except the newline character (n, r). < / td > < / tr > < tr > < td >\ w < / td > < td align= "center" > matches uppercase and lowercase letters, or numbers, or underscores . Equivalent to [A-Za-z0-9]. < / td > < / tr > < tr > < td >\ W < / td > < td align= "center" > is the opposite of the lowercase w. Equivalent to < sup id= "fnref-1" > 1 < / sup >. < / td > < / tr > < tr > < td >\ s < / td > < td align= "center" > matches any white space characters, including spaces, tabs, page feeds, and so on. Equivalent to [fnrtv]. < / td > < / tr > < tr > < td >\ S < / td > < td align= "center" > is the opposite of the lowercase s. Equivalent to < sup id= "fnref-2" > 2 < / sup >. < / td > < / tr > < tr > < td >\ d < / td > < td align= "center" > matches a numeric character, which is equivalent to [0-9]. < / td > < / tr > < tr > < td >\ D < / td > < td align= "center" > matches a non-numeric character, which is equivalent to < sup id= "fnref-3" > 3 < / sup >. < / td > < / tr > < tr > < td >\ b < / td > < td align= "center" > matches the boundary of a word, that is, the position of the word and spaces.
for example, 'erb' can match' er' in 'er', but not matching' verb' in 'never'. < / td > < / tr > < tr > < td > ^ < / td > < td align= "center" > matches the starting position of the input string. < / td > < / tr > < tr > < td > $< / td > < td align= "center" > matches the end of the input string. < / td > < / tr > < tr > < td > [] < / td > < td align= "center" > matches the characters enumerates in []. < / td > < / tr > < / tbody > < / table >

2. Then, let's look at the character

that represents the regular repeats . < table > < thead > < tr > < th > Code / Syntax < / th > < th > description < / th > < / tr > < / thead > < tbody > < tr > < td > * < / td > < td > match the previous subexpression zero or more times repeatedly < / td > < / tr > < tr > < td > + < / td > < td > repeatedly matches the previous subexpression one or more times < / td > < / tr > < tr > < td >? < / td > < td > repeatedly matches the previous subexpression zero or once < / td > < / tr > < tr > < td > {n} < / td > < td > repeat the previous subexpression n times < / td > < / tr > < tr > < td > {n,} < / td > < td > repeatedly match the previous subexpression n or more times < / td > < / tr > < tr > < td > {n < m} < / td > < td > repeat the previous subexpression n to m times < / td > < / tr > < / tbody > < / table >

3, according to your example, what we need is such a regular:

ret=re.search(r'\d+','hello999')
print(ret.group())

4. Now let's talk about why your regularities are not successful

4.1.First, take a look at the definition of the re.search function

re.search scans the entire string and returns the first successful match of .

matches start at the far left of the string, and as long as the match reaches the first one, it is returned, regardless of anything else.

4.2, look at your regularity

ret=re.search(r'd*','hello999')
print(ret.group())

you use a ringing strong character, which means that the string d appears zero or more times. Remember the concept of zero < / string >, which is an empty character. Your rule matches the leftmost empty character of hello.

of course, I understand that the subject means to match the numeric character , but unfortunately, only\ d is equivalent to [0-9], d is just the string d.

to express one or more implementations, you have to use +,
, so the final expression should be r', dashes'.

< hr >
    < li id= "fn-1" > A-Za-z0-9 _ please < li id= "fn-2" > fnrtv please < li id= "fn-3" > 0-9 please
Menu