PART-I:
PART-II:
Lecture 2.18 (Regular Expressions in Python)
Special characters are characters that do not match themselves as seen but have a special meaning when used in a regular expression. Some commonly used wild cards or meta characters are listed below:
Wild Card | Description |
---|---|
^ | Caret symbol specifies that the match must start at the beginning of the string, and in MULTILINE mode also matches immediately after each newline - ^b will check if the string starts with 'b' such as baba, boss, basic, b, by, etc.- ^si will check if the string starts with 'si' such as simple, sister, si, etc. |
$ | Specifies that the match must occur at the end of the string - s$ will check for the string that ends with a such as geeks, ends, s, etc.- ing$ will check for the string that ends with ing such as going, seeing, ing, etc. |
. | Represent a single occurrance of any character except new line - a.b will check for the string that contains any character at the place of the dot such as acb, acbd, abbb, etc- .. will check if the string contains at least 2 characters |
*\* | Used to drop special meaning of a character following it or used to refer to a special character. - Since dot (.) is a metacharacter, so if you want to search it in a string you have to use the backslash (\) just before the dot (.) so that it will lose its specialty. |
[...] | Matches a single character in the listed set. If caret is the first character inside it, it means negation - [abc] means match any single character out of this set- [123] means match any single digit out of this set- [a-z] means match any single character out of lower case alphabets- [0-9] means match any single digit out of this set- [^0-3] means any number except 0, 1, 2, or 3- [^a-c] means any character except a, b, or c- [0-5][0-9] will match all the two-digits numbers from 00 to 59 - [0-9A-Fa-f] will match any hexadecimal digit.- Special characters lose their special meaning inside sets, so [(+*)] will match any of the literal characters '(', '+', '*', or ')'.- To match a literal ']' inside a set, precede it with a backslash, or place it at the beginning of the set, so [()[\]{}] and []()[{}] will both match parenthesis. |
^[...] | Matches any character in the set at the beginning of the string |
[^...] | Matches any character except those NOT in the listed set (negation) |
| | Or symbol works as the OR operator meaning it checks whether the pattern before or after the or symbol is present in the string or not - a|b will match any string that contains a or b such as acd, bcd, abcd, etc.- To match a literal '|', use | , or enclose it inside a character class, as in [|] . |
( ) | Used to capture and group |
Wild Card | Description |
---|---|
***** | The preceding character/expression is repeated zero or more times |
+ | The preceding character/expression is repeated one or more times, - ab+c will be matched for the string abc, abbc, dabc, but will not be matched for ac, abdc because there is no b in ac and d is not followed by c in abdc. |
? | The preceding character/expression is optional (zero or one occurrence). - ab?c will be matched for the string ac, abc, acb, dabc, dac but will not be matched for abbc because there are two b. Similarly, it will not be matched for abdc because b is not followed by c. |
{n,m} | The preceding character/expression is repeated from n to m times (both enclusive). - a{2,4} will be matched for the string aaab, baaaac, gaad, but will not be matched for strings like abc, bc because there is only one a or no a in both the cases. |
{n} | The preceding character/expression is repeated n times. - a{6} will match exactly six 'a' characters, but not five. |
{n,} | The preceding character/expression is repeated atleast n times |
{,m} | The preceding character/expression is repeated upto m times |
Code | Description |
---|---|
\d | Matches any decimal digit. This is equivalent to [0-9] |
\D | Matches any non-digit character. This is equivalent to [^0-9] or [^\d] |
\s | Matches any whitespace character. This is equivalent to [ \r\n\t\b\f] |
\S | Matches any non-whitespace character. This is equivalent to [^ \r\t\n\f] or [^\s] |
\w | Matches alphanumeric character. This is equivalent to [a-zA-Z0-9_] |
\W | Matches any non-alphanumeric character. This is equivalent to [^a-zA-Z0-9_] or [^\w] |
\b | Matches where the specified characters are at the beginning or at the end of a word r"\bain" OR r"ain\b" |
\B | Matches where the specified characters are present, but NOT or at the end of a word r"Bain" OR r"ain\B" |
(Visit reges101)[https://regex101.com/]
abcdefghijklmnopqurtuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ 1234567890 Ha HaHa MetaCharacters (Need to be escaped): .[{()^$|?*+ arifbutt.me 321-555-4321 123.555.1234 111#923#9234 cat mat bat 0x45 0X4Ad 0x2g3 0x349ABf 0x
Hello World Mr. Shahzad Mr Khurram Ms Aqsa Mrs. Shaista Mr. B Learning is fun
List of Valid Email Addresses arif@pucit.edu.pk arif.ds@pu.edu.pk arifpucit@gmail.com arif.pucit@pu.edu.pk first+123.5@example.com abc%xyz@subdomain.example.com my_name@example.com first-last@example.com
List of Invalid Email Addresses
abc.def@mail abc.def@mail#archive.com @example.com arif butt @example.com khurram#@gmail.com Abc.example.com
def myfa(mynumb):
oddbits = 0
evenbits = 0
for i in range(len(mynumb)):
if (mynumb[i] == '1'):
if (i % 2 == 0):
evenbits += 1
else:
oddbits += 1
if (abs(oddbits - evenbits) % 3 == 0):
print("Yes")
else:
print("No")
myfa("1011111")
No
^(0|(1(01*0)*10*)+)$
File "/var/folders/1t/g3ylw8h50cjdqmk5d6jh1qmm0000gn/T/ipykernel_14988/3412454348.py", line 1 ^(0|(1(01*0)*10*)+)$ ^ SyntaxError: invalid syntax
0
1
10
11
100
101
110
111
1000
1001
1010
1011
1100
1101
1110
1111
10000
10001
10010
10011
10100
10101
10110
10111
11000
11001
11010
11011
11100
11101
11110
11111