|
1 | 1 | {
|
2 |
| - "1a": "Examples for Python regular expressions are shown below. You can modify such code snippets and pressing the **Enter** key will update the corresponding output.\n\nAs a good practice, always use **raw strings** to construct the pattern, unless other formats are required. This will avoid conflict between special meaning of the backslash character in regular expressions and string literals.\n\n### Examples for `re.search()`\n\n", |
| 2 | + "1a": "Examples for Python regular expressions are shown below. You can modify such code snippets and pressing the [bold]Enter[/] key will update the corresponding output.\n\nAs a good practice, always use [bold]raw strings[/] to construct the pattern, unless other formats are required. This will avoid conflict between special meaning of the backslash character in regular expressions and string literals.\n\n[bold]Examples for [dark_orange3 on grey84]re.search()[/][/]\n", |
3 | 3 | "1b": [
|
4 | 4 | "sentence = 'This is a sample string'\n# check if 'sentence' contains the pattern described by RE argument\nbool(re.search(r'is', sentence))",
|
5 | 5 | "# ignore case while searching for a match\nbool(re.search(r'this', sentence, flags=re.I))",
|
6 | 6 | "# example when pattern isn't found in the input string\nbool(re.search(r'xyz', sentence))",
|
7 | 7 | "# use raw byte strings for patterns if input is of byte data type\nbool(re.search(rb'is', b'This is a sample string'))\n"
|
8 | 8 | ],
|
9 |
| - "2a": "\n### String and Line anchors\n\n", |
| 9 | + "2a": "[bold]String and Line anchors[/]\n", |
10 | 10 | "2b": [
|
11 | 11 | "# match the start of the input string\nbool(re.search(r'\\Ahi', 'hi hello\\ntop spot'))",
|
12 | 12 | "# match the start of a line\nbool(re.search(r'^top', 'hi hello\\ntop spot', flags=re.M))",
|
13 | 13 | "# match the end of strings\nwords = ['surrender', 'up', 'newer', 'do', 'era', 'eel', 'pest']\n[w for w in words if re.search(r'er\\Z', w)]",
|
14 | 14 | "# check if there's a whole line 'par'\nbool(re.search(r'^par$', 'spare\\npar\\ndare', flags=re.M))\n"
|
15 | 15 | ],
|
16 |
| - "3a": "\n### Examples for `re.findall()`\n\n", |
| 16 | + "3a": "[bold]Examples for [dark_orange3 on grey84]re.findall()[/][/]\n", |
17 | 17 | "3b": [
|
18 | 18 | "# match 'par' with optional 's' at start and optional 'e' at end\nre.findall(r'\\bs?pare?\\b', 'par spar apparent spare part pare')",
|
19 | 19 | "# numbers >= 100 with optional leading zeros\n# Python 3.11 supports possessive quantifiers\n# re.findall(r'\\b0*+\\d{3,}\\b', '0501 035 154 12 26 98234')\nre.findall(r'\\b0*[1-9]\\d{2,}\\b', '0501 035 154 12 26 98234')",
|
20 | 20 | "# if multiple capturing groups are used, each element of output\n# will be a tuple of strings of all the capture groups\nre.findall(r'([^/]+)/([^/,]+),?', '2020/04,1986/Mar')",
|
21 | 21 | "# normal capture group will hinder ability to get the whole match\n# non-capturing group to the rescue\nre.findall(r'\\b\\w*(?:st|in)\\b', 'cost akin more east run against')",
|
22 | 22 | "# useful for debugging purposes as well\nre.findall(r':.*?:', 'green:3.14:teal::brown:oh!:blue')\n"
|
23 | 23 | ],
|
24 |
| - "4a": "\n### Examples for `re.split()`\n\n", |
| 24 | + "4a": "[bold]Examples for [dark_orange3 on grey84]re.split()[/][/]\n", |
25 | 25 | "4b": [
|
26 | 26 | "# split based on one or more digit characters\nre.split(r'\\d+', 'Sample123string42with777numbers')",
|
27 | 27 | "# split based on digit or whitespace characters\nre.split(r'[\\d\\s]+', '**1\\f2\\n3star\\t7 77\\r**')",
|
28 | 28 | "# to include the matching delimiter strings as well in the output\nre.split(r'(\\d+)', 'Sample123string42with777numbers')",
|
29 | 29 | "# multiple capture groups example\n# note that the portion matched by b+ isn't present in the output\nre.split(r'(a+)b+(c+)', '3.14aabccc42')",
|
30 | 30 | "# use non-capturing group if capturing is not needed\nre.split(r'hand(?:y|ful)', '123handed42handy777handful500')\n"
|
31 | 31 | ],
|
32 |
| - "5a": "\n### Backreferencing within the search pattern\n\n", |
| 32 | + "5a": "[bold]Backreferencing within the search pattern[/]\n", |
33 | 33 | "5b": [
|
34 | 34 | "# whole words that have at least one consecutive repeated character\nwords = ['effort', 'flee', 'facade', 'oddball', 'rat', 'tool']\n[w for w in words if re.search(r'\\b\\w*(\\w)\\1\\w*\\b', w)]\n"
|
35 | 35 | ],
|
36 |
| - "6a": "\n### Working with matched portions\n\n", |
| 36 | + "6a": "[bold]Working with matched portions[/]\n", |
37 | 37 | "6b": [
|
38 | 38 | "# re.Match object\nre.search(r'so+n', 'too soon a song snatch')",
|
39 | 39 | "# retrieving entire matched portion, note the use of [0]\nmotivation = 'Doing is often better than thinking of doing.'\nre.search(r'of.*ink', motivation)[0]",
|
40 | 40 | "# capture group example\npurchase = 'coffee:100g tea:250g sugar:75g chocolate:50g'\nm = re.search(r':(.*?)g.*?:(.*?)g.*?chocolate:(.*?)g', purchase)\n# to get the matched portion of the second capture group\nm[2]",
|
41 | 41 | "# to get a tuple of all the capture groups\nm.groups()\n"
|
42 | 42 | ],
|
43 |
| - "7a": "\n### Examples for `re.finditer()`\n\n", |
| 43 | + "7a": "[bold]Examples for [dark_orange3 on grey84]re.finditer()[/][/]\n", |
44 | 44 | "7b": [
|
45 | 45 | "# numbers < 350\nm_iter = re.finditer(r'\\d+', '45 349 651 593 4 204 350')\n[m[0] for m in m_iter if int(m[0]) < 350]",
|
46 | 46 | "# start and end+1 index of each matching portion\nm_iter = re.finditer(r'so+n', 'song too soon snatch')\n[m.span() for m in m_iter]\n"
|
47 | 47 | ],
|
48 |
| - "8a": "\n### Examples for `re.sub()`\n\n", |
| 48 | + "8a": "[bold]Examples for [dark_orange3 on grey84]re.sub()[/][/]\n", |
49 | 49 | "8b": [
|
50 | 50 | "# add something to the start of every line\nip_lines = \"catapults\\nconcatenate\\ncat\"\nre.sub(r'^', r'* ', ip_lines, flags=re.M).splitlines(True)",
|
51 | 51 | "# replace 'par' only at the start of a word\nre.sub(r'\\bpar', r'X', 'par spar apparent spare part')",
|
52 | 52 | "# same as: r'part|parrot|parent'\nre.sub(r'par(en|ro)?t', r'X', 'par part parrot parent')",
|
53 | 53 | "# remove the first two columns where : is delimiter\nre.sub(r'\\A([^:]+:){2}', '', 'apple:123:banana:cherry')\n"
|
54 | 54 | ],
|
55 |
| - "9a": "\n### Backreferencing in the replacement section\n\n", |
| 55 | + "9a": "[bold]Backreferencing in the replacement section[/]\n", |
56 | 56 | "9b": [
|
57 | 57 | "# remove any number of consecutive duplicate words separated by space\n# use \\W+ instead of space to cover cases like 'a;a<-;a'\nre.sub(r'\\b(\\w+)( \\1)+\\b', r'\\1', 'aa a a a 42 f_1 f_1 f_13.14')",
|
58 | 58 | "# add something around the matched strings\nre.sub(r'\\d+', r'(\\g<0>0)', '52 apples and 31 mangoes')",
|
59 | 59 | "# swap words that are separated by a comma\nre.sub(r'(\\w+),(\\w+)', r'\\2,\\1', 'good,bad 42,24')",
|
60 | 60 | "# example with both capturing and non-capturing groups\nre.sub(r'(\\d+)(?:abc)+(\\d+)', r'\\2:\\1', '1000abcabc42 12abcd21')\n"
|
61 | 61 | ],
|
62 |
| - "10a": "\n### Using functions in the replacement section of `re.sub()`\n\n", |
| 62 | + "10a": "[bold]Using functions in the replacement section of [dark_orange3 on grey84]re.sub()[/][/]\n", |
63 | 63 | "10b": [
|
64 | 64 | "# factorial is imported from the math module\nnumbers = '1 2 3 4 5'\ndef fact_num(m): return str(factorial(int(m[0])))\nre.sub(r'\\d+', fact_num, numbers)",
|
65 | 65 | "# using lambda\nre.sub(r'\\d+', lambda m: str(factorial(int(m[0]))), numbers)\n"
|
66 | 66 | ],
|
67 |
| - "11a": "\n### Examples for lookarounds\n\n", |
| 67 | + "11a": "[bold]Examples for lookarounds[/]\n", |
68 | 68 | "11b": [
|
69 | 69 | "# change 'cat' only if it is not followed by a digit character\n# note that the end of string satisfies the given assertion\n# 'catcat' has two matches as the assertion doesn't consume characters\nre.sub(r'cat(?!\\d)', 'dog', 'hey cats! cat42 cat_5 catcat')",
|
70 | 70 | "# change whole word only if it is not preceded by : or -\nre.sub(r'(?<![:-])\\b\\w+\\b', r'X', ':cart <apple -rest ;tea')",
|
|
73 | 73 | "# match if 'do' is not there between 'at' and 'par'\nbool(re.search(r'at((?!do).)*par', 'fox,cat,dog,parrot'))",
|
74 | 74 | "# match if 'go' is not there between 'at' and 'par'\nbool(re.search(r'at((?!go).)*par', 'fox,cat,dog,parrot'))\n"
|
75 | 75 | ],
|
76 |
| - "12a": "\n### Examples for `re.compile()`\n\nRegular expressions can be compiled using the `re.compile()` function, which gives back a `re.Pattern` object. The top level `re` module functions are all available as methods for this object. Compiling a regular expression helps if the RE has to be used in multiple places or called upon multiple times inside a loop (speed benefit). By default, Python maintains a small list of recently used RE, so the speed benefit doesn't apply for trivial use cases.\n\n", |
| 76 | + "12a": "[bold]Examples for [dark_orange3 on grey84]re.compile()[/][/]\n\nRegular expressions can be compiled using the [dark_orange3 on grey84]re.compile()[/] function, which gives back a [dark_orange3 on grey84]re.Pattern[/] object. The top level [dark_orange3 on grey84]re[/] module functions are all available as methods for this object. Compiling a regular expression helps if the RE has to be used in multiple places or called upon multiple times inside a loop (speed benefit). By default, Python maintains a small list of recently used RE, so the speed benefit doesn't apply for trivial use cases.\n", |
77 | 77 | "12b": [
|
78 | 78 | "pet = re.compile(r'dog')\ntype(pet)",
|
79 | 79 | "bool(pet.search('They bought a dog'))",
|
|
0 commit comments