in python you use Regular Expressions then first import re modules
The special characters are:
"." | Matches any character except a newline. |
"^" | Matches the start of the string. |
"$" | Matches the end of the string or just before the newline at the end of the string. |
"*" | Matches 0 or more occurrences |
"+" | Matches 1 or more occurrences. |
"?" | Matches 0 or 1 occurrences. |
*?,+?,?? | Non-greedy versions of the previous three special characters. |
{m,n} | Matches from m to n repetitions of the preceding RE. |
{m,n}? | Non-greedy version of the above. |
"\\" | Either escapes special characters or signals a special sequence. |
[] | Indicates a set of characters.A "^" as the first character indicates a complementing set. |
"|" | A|B, creates an RE that will match either A or B. |
(...) | Matches the RE inside the parentheses.The contents can be retrieved or matched later in the string. |
(?aiLmsux) | Set the A, I, L, M, S, U, or X flag for the RE (see below). |
(?:...) | Non-grouping version of regular parentheses. |
(?P |
The substring matched by the group is accessible by name. |
(?P=name) | Matches the text matched earlier by the group named name. |
(?#...) | A comment; ignored. |
(?=...) | Matches if ... matches next, but doesn't consume the string. |
(?!...) | Matches if ... doesn't match next. |
(?<=...) | Matches if preceded by ... (must be fixed length). |
This module exports the following functions:
match | Match a regular expression pattern to the beginning of a string. |
search | Search a string for the presence of a pattern. |
findall | Find all occurrences of a pattern in a string. |
import re def getRegularExpressions(): searchKeyword=input("Enter the Search keyword :") se=re.match(searchKeyword,"I'm not a great programmer; I'm just a good programmer with great habits.") print(se) getRegularExpressions()
Enter the Search keyword :I'm
import re def getRegularExpressions(): searchKeyword=input("Enter the Search keyword :") se=re.match(searchKeyword,"I'm not a great programmer; I'm just a good programmer with great habits.") print(se.group()) getRegularExpressions()
Enter the Search keyword :I'm I'm
import re def getRegularExpressions(): searchKeyword=input("Enter the Search keyword :") se=re.match(searchKeyword,"I'm not a great programmer; I'm just a good programmer with great habits.") print(se.start()," ",se.end()) getRegularExpressions()
Enter the Search keyword :I'm 0 3
import re def getRegularExpressions(): searchKeyword=input("Enter the Search keyword :") se=re.match(searchKeyword,"I'm not a great programmer; I'm just a good programmer with great habits.") print(se.span()) getRegularExpressions()
Enter the Search keyword :I'm (0, 3)
import re def getRegularExpressions(): searchKeyword=input("Enter the Search keyword :") se=re.match(searchKeyword,"I'm not a great programmer; I'm just a good programmer with great habits.") print(se) getRegularExpressions()
Enter the Search keyword :ashu None
import re def getRegularExpressions(): searchKeyword=input("Enter the Search keyword :") se=re.search(searchKeyword,"I'm not a great programmer; I'm just a good programmer with great habits.") print(se) getRegularExpressions()
Enter the Search keyword :programmer
import re def getRegularExpressions(): searchKeyword=input("Enter the Search keyword :") se=re.search(searchKeyword,"I'm not a great programmer; I'm just a good programmer with great habits.") print(se.group()) getRegularExpressions()
Enter the Search keyword :programmer programmer
import re def getRegularExpressions(): searchKeyword=input("Enter the Search keyword :") se=re.search(searchKeyword,"I'm not a great programmer; I'm just a good programmer with great habits.") print(se.start()," ",se.end()) getRegularExpressions()
Enter the Search keyword :programmer 16 26
import re def getRegularExpressions(): searchKeyword=input("Enter the Search keyword :") se=re.search(searchKeyword,"I'm not a great programmer; I'm just a good programmer with great habits.") print(se.span()) getRegularExpressions()
Enter the Search keyword :programmer (16, 26)
import re def getRegularExpressions(): searchKeyword=input("Enter the Search keyword :") se=re.search(searchKeyword,"I'm not a great programmer; I'm just a good programmer with great habits.") print(se) getRegularExpressions()
Enter the Search keyword :ashu None
import re def getRegularExpressions(): searchKeyword=input("Enter the Search keyword :") se=re.findall(searchKeyword,"I'm not a great programmer; I'm just a good programmer with great habits.") print(se) getRegularExpressions()
Enter the Search keyword :programmer ['programmer', 'programmer']
import re def getRegularExpressions(): print(re.match("[abc]","abc")) print(re.match("[abc]","bca")) print(re.match("[abc]","cab")) print(re.match("[a-z]","aaaa")) print(re.match("[0-9]","111")) print(re.match("[123]","333")) getRegularExpressions()
import re def getRegularExpressions(): print(re.match("[abc]*","")) print(re.match("[abc]*",".")) print(re.match("[abc]*","xyz")) print(re.match("[abc]*","cab")) print(re.match("[a-z]*","aaaa")) print(re.match("[0-9]*","111")) print(re.match("[123]*","333")) getRegularExpressions()
import re def getRegularExpressions(): print(re.match("[abc]+","")) print(re.match("[abc]+",".")) print(re.match("[abc]+","xyz")) print(re.match("[abc]+","cab")) print(re.match("[a-z]+","aaaa")) print(re.match("[0-9]+","111")) print(re.match("[123]+","333")) getRegularExpressions()
None None None
import re def getRegularExpressions(): print(re.match("[abc]?","")) print(re.match("[abc]?",".")) print(re.match("[abc]?","xyz")) print(re.match("[abc]?","cab")) print(re.match("[a-z]?","aaaa")) print(re.match("[0-9]?","111")) print(re.match("[123]?","333")) getRegularExpressions()