{"id":2203,"date":"2024-05-30T04:21:03","date_gmt":"2024-05-30T11:21:03","guid":{"rendered":"https:\/\/gantovnik.com\/bio-tips\/?p=2203"},"modified":"2024-05-30T04:23:40","modified_gmt":"2024-05-30T11:23:40","slug":"429-pattern-matching-with-regular-expressions-grouping-with-parentheses","status":"publish","type":"post","link":"https:\/\/gantovnik.com\/bio-tips\/2024\/05\/429-pattern-matching-with-regular-expressions-grouping-with-parentheses\/","title":{"rendered":"#429 Pattern matching with regular expressions: grouping with parentheses"},"content":{"rendered":"<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nimport re\r\nphoneNumRegex = re.compile(r&#039;(\\d\\d\\d)-(\\d\\d\\d-\\d\\d\\d\\d)&#039;)\r\nmo = phoneNumRegex.search(&#039;Phone number: 415-555-4242&#039;)\r\nprint(mo.group(1))\r\nprint(mo.group(2))\r\nprint(mo.group(0))\r\nprint(mo.groups())\r\n<\/pre>\n<p>Output:<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\n415\r\n555-4242\r\n415-555-4242\r\n(&#039;415&#039;, &#039;555-4242&#039;)\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>import re phoneNumRegex = re.compile(r&#039;(\\d\\d\\d)-(\\d\\d\\d-\\d\\d\\d\\d)&#039;) mo = phoneNumRegex.search(&#039;Phone number: 415-555-4242&#039;) print(mo.group(1)) print(mo.group(2)) print(mo.group(0)) print(mo.groups()) Output: 415 555-4242 415-555-4242 (&#039;415&#039;, &#039;555-4242&#039;)<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"nf_dc_page":"","_et_pb_use_builder":"","_et_pb_old_content":"","_et_gb_content_width":"","_lmt_disableupdate":"yes","_lmt_disable":"","jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[2,109],"tags":[3,110],"class_list":["post-2203","post","type-post","status-publish","format-standard","hentry","category-python","category-regexp","tag-python","tag-regexp"],"modified_by":"gantovnik","jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p8bH0k-zx","jetpack_likes_enabled":true,"jetpack-related-posts":[{"id":2131,"url":"https:\/\/gantovnik.com\/bio-tips\/2024\/02\/415-tight_layout-in-matplotlib-in-python\/","url_meta":{"origin":2203,"position":0},"title":"#415 tight_layout() in matplotlib in python","author":"gantovnik","date":"2024-02-20","format":false,"excerpt":"[code language=\"python\"] import matplotlib.pyplot as plt import numpy as np fig, (ax1, ax2, ax3) = plt.subplots(1, 3, figsize=(10,3)) xx = np.linspace(1, 20, 1000) yy = np.log(xx**2 + 1) * np.sin(xx) + xx ax1.plot(xx, yy) ax2.scatter(sorted(xx), sorted(yy), s=1, marker=\".\", color=\"green\") ax3.hist(yy, bins=20, color=\"red\",edgecolor='black', alpha=.5) fig.tight_layout() plt.savefig(\"ex415.png\", dpi=100) plt.show() [\/code]","rel":"","context":"In &quot;matplotlib&quot;","block_context":{"text":"matplotlib","link":"https:\/\/gantovnik.com\/bio-tips\/category\/matplotlib\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2024\/02\/ex415.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2024\/02\/ex415.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2024\/02\/ex415.png?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2024\/02\/ex415.png?resize=700%2C400&ssl=1 2x"},"classes":[]},{"id":10320,"url":"https:\/\/gantovnik.com\/bio-tips\/2025\/08\/462-sorting-a-dictionary-by-key-or-value-in-python\/","url_meta":{"origin":2203,"position":1},"title":"#462 Sorting a Dictionary by Key or Value in Python","author":"gantovnik","date":"2025-08-24","format":false,"excerpt":"In Python, dictionaries are unordered collections of key-value pairs. Sometimes, we need to sort a dictionary either by its keys or by its values. This can be done easily using the built-in sorted() function. # Example dictionary d = {'a': 3, 'c': 1, 'b': 2} # Sort dictionary items by\u2026","rel":"","context":"In &quot;python&quot;","block_context":{"text":"python","link":"https:\/\/gantovnik.com\/bio-tips\/category\/python\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":2634,"url":"https:\/\/gantovnik.com\/bio-tips\/2024\/07\/436-non-convex-univariate-function-optimization-using-brents-method-in-python\/","url_meta":{"origin":2203,"position":2},"title":"#436 Non-convex univariate function optimization using Brent&#8217;s method in python","author":"gantovnik","date":"2024-07-18","format":false,"excerpt":"Brent\u2019s method is an optimization algorithm that combines a bisecting algorithm (Dekker\u2019s method) and inverse quadratic interpolation. It can be used for constrained and unconstrained univariate function optimization. The Brent-Dekker method is an extension of the bisection method. It is a root-finding algorithm that combines elements of the secant method\u2026","rel":"","context":"In &quot;optimization&quot;","block_context":{"text":"optimization","link":"https:\/\/gantovnik.com\/bio-tips\/category\/optimization\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2024\/07\/ex436.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2024\/07\/ex436.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2024\/07\/ex436.png?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":1112,"url":"https:\/\/gantovnik.com\/bio-tips\/2021\/11\/194-solving-of-system-of-linear-equations-in-python-gauss-seidel-method\/","url_meta":{"origin":2203,"position":3},"title":"#194 Solving of system of linear equations in python (Gauss-Seidel method)","author":"gantovnik","date":"2021-11-19","format":false,"excerpt":"[code language=\"python\"] import numpy as np # solve system of linear equation using # Gauss-Seidel method using # a predefined threshold eps=0.01 # 8*x1+3*x2-3*x3=14 # -2*x1-8*x2+5*x3=5 # 3*x1+5*x2+10*x3=-8 # first check if the coefficient matrix is # diagonally dominant or not. a = [[8, 3, -3], [-2, -8, 5], [3,\u2026","rel":"","context":"In &quot;python&quot;","block_context":{"text":"python","link":"https:\/\/gantovnik.com\/bio-tips\/category\/python\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1742,"url":"https:\/\/gantovnik.com\/bio-tips\/2023\/01\/204-mandelbrot-fractal-using-python-2-2-2-2-2-2-2-2-2\/","url_meta":{"origin":2203,"position":4},"title":"#330 Gradient method using python","author":"gantovnik","date":"2023-01-03","format":false,"excerpt":"golden.py [code language=\"python\"] import math as mt def golden(f,a,b,tol=1.0e-10): # Golden section method for determining x # that minimizes the scalar function f(x). # The minimum must be bracketed in (a,b). c1 = (mt.sqrt(5.0)-1.0)\/2.0 c2 = 1.0 - c1 nIt = int(mt.ceil(mt.log(tol\/abs(a-b))\/mt.log(c1))) # first step x1 = c1*a + c2*b\u2026","rel":"","context":"In &quot;optimization&quot;","block_context":{"text":"optimization","link":"https:\/\/gantovnik.com\/bio-tips\/category\/optimization\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2023\/01\/ex330.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":200,"url":"https:\/\/gantovnik.com\/bio-tips\/2019\/01\/heat-equation-1d\/","url_meta":{"origin":2203,"position":5},"title":"Heat equation (1D)","author":"gantovnik","date":"2019-01-10","format":false,"excerpt":"import os import numpy as np import matplotlib.pyplot as plt os.chdir(r'D:\\projects\\wordpress\\ex41') os.getcwd() N = 5 u0 = 1 u1 = 2 dx = 1.0 \/ (N + 1) A = (np.eye(N, k=-1) - 2 * np.eye(N) + np.eye(N, k=1)) \/ dx**2 print(A) d = -5 * np.ones(N) d[0] -= u0\u2026","rel":"","context":"In &quot;python&quot;","block_context":{"text":"python","link":"https:\/\/gantovnik.com\/bio-tips\/category\/python\/"},"img":{"alt_text":"example41","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example41.png?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example41.png?resize=350%2C200 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example41.png?resize=525%2C300 1.5x"},"classes":[]}],"_links":{"self":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts\/2203","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/comments?post=2203"}],"version-history":[{"count":2,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts\/2203\/revisions"}],"predecessor-version":[{"id":2205,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts\/2203\/revisions\/2205"}],"wp:attachment":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/media?parent=2203"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/categories?post=2203"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/tags?post=2203"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}