Next, you will learn about anchors in regular expression. Anchors are used to specify the start and end of the string. Watch the following video where Krishna explains what are anchors and how to use them.
Note:
There is a correction in the video. in the comment of the python code,
print(find_ pattern(“India”, “a$”)) #return true if string ends with ‘a’ and not ‘c’.
print (find_ pattern (“Japan”, “a$”)) # return true if string ends with ‘a’ and not ‘c’.
you learnt about the two anchors characters: ‘^’ and ‘$’.
The’^’ specifies the start of the string. the character followed by the ‘^’ in the pattern should be the first character of the string in order for a string to match the pattern.
Both the anchors can be specified in a single regular expression itself. for example, the regular expression pattern ‘^01*0$’ will match any string that starts and end with zeroes with any number of is between them. it will match’010′, ‘0110’ ‘0111111111110’ and even ’00’ (‘+’ matches zero or more is) . But will not match the string ‘o’ because there is only one zero in this string and in the pattern we have specified that there needs to be two os, one at the start and one at the end.
Now, there is one special character in regular expressions that acts as a placeholder and can match any character(literally!) in the given input string. It’s the ‘.'(dot) character is also called the Wildcard character.
Till now, you were mentioning the exact character followed by a quantifier in your regular expression patterns. For example, the pattern’ hur{2,}ay’ matcher ‘hurrray’, ‘ hurrray’, hurrry’ and so on. here, we had
specified that the letter ‘r’ should be present two or more times. But you don’t always know the letter that you want to repeat. In such situations, you’ll need to use the wildcard instead.
The wildcard comes handy in many situations. It can be followed by a quantifier which specifies that any character is present a specified number of times.
For example, if you’re asked to write a regex pattern that should match a string that starts with four characters, followed by three 0s and two 1s, followed by any two characters. The valid strings can be abcd00011ft, jkds00011hf, etc. The pattern that satisfies this kind of condition would be ‘.{4}0{3}1{2}.{2}’. You can also use ‘….00011..’ where the dot acts as a placeholder which means anything can sit on the place of the dot. Both are correct regex patterns.
So, you learnt how the ‘.’ character can act as a placeholder for any character and how to use it. In the next section, you’ll learn about character sets.