Created
August 3, 2026 20:09
-
-
Save me-suzy/cf14e275623056b934a6ad49d1cb93c6 to your computer and use it in GitHub Desktop.
regex GENERIC FORMULAS.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| GENERIC FORMULAS | |
| ------------------ | |
| Lista Generic Regex: | |
| (?:\G(?!\A)|^.*?(?:BSR))(?:(?!ESR).)*?\K(?:FR) / REPLACE RR | |
| (?:.*?BSR|\G).*?FR\K(.*?)(?=ESR.*?ESR) | |
| (?-si:BSR|(?!\A)\G)(?s-i:(?!ESR).)*?\KFR | |
| (BSR)\s*FR(.*?)FR\s*(ESR) Replace by: \1\2\3 | |
| (?s-i)(?:BSR|(?!\A)\G)(?:(?!ESR).)*?\K(FR) | |
| (BSR.*?)(FR\s*ESR) | |
| (?:BSR|\G).*?\K(?:FR(*SKIP)(*FAIL)|ESR) | |
| (?:BSR|\G).*?\K(?:FR(*SKIP)(*FAIL)|<(?:(?!ESR).)+?>) | |
| (?:BSR|\G).*?\K(?:FR(*SKIP)(*FAIL)|<(?:(?!ESR1|ESR2).)+?>) | |
| BSR(?:[^<]+|<(?!FR1|FR2))?</p>(*SKIP)(FAIL)|<(?!FR1|FR2)[^>]+> | |
| BSR(?:[^<]+|<(?!FR))?</p>(*SKIP)(FAIL)|<(?!FR)[^>]+> | |
| (?-s)(?-i:BSR|(?!\A)\G)(?s-i:.*?ESR)(*SKIP)(*FAIL)|FR | |
| (?s)(?-i:BSR|(?!\A)\G).*?\K(?!ESR) | |
| (?-si:BSR|(?!\\A)\\G)(?s-i:(?!ESR).)*?\\x20\\K(FR) | |
| (?-si:BSR|(?!\\A)\\G)(?s-i:(?!ESR).)*?\\x20\\KFR(?=\\x20) | |
| (?-si:BSR|(?!\\A)\\G)(?s-i:(?!ESR).)*?\\x20\\KFR | |
| (?-si:BSR|(?!\\A)\\G)(?s-i:(?!ESR).)*?\\x20\\KFR(?=\\x20) | |
| (?s-i)(?:BSR|(?!\A)\G)(?:(?!ESR).)*?\K<(?!(?:FR1|FR2))[^>]*> | |
| (?s)(?-i:BSR|(?!\A)\G).*?\K(?<=FR1)(.*?)(?=FR2)(?!ESR) | |
| (?s)(?-i:BSR).*?(?<=FR1)(.*?)(?=FR2)(?=.*?ESR) | |
| BSR[\s\S]+FR1\s+(\S+)\s+FR2[\s\S]+ESR | |
| (?s)(?<=BSR[\s\S]+FR1\s+)(\S+)(?=\s+FR2[\s\S]+ESR) | |
| (?s)BSR[\s\S]+FR1\s+(\S+)\s+FR2[\s\S]+ESR | |
| (?-s)(?-i:BSR|(?!\A)\G)(?s-i:.*?ESR)(*SKIP)(*FAIL)|FR | |
| (?-s)(?-i:BSR|(?!\A)\G).*?\KFR(?!ESR) | |
| (?-s)(?-i:BSR|(?!\A)\G)(?s-i:(?!ESR).)*?\K(?-si:FR) | |
| (?-s)(?-i:BSR|(?!\A)\G).*?\K(?-si:FR)(?=(?s-i:.*?ESR)) | |
| (?s)BSR(FR)(?-s).+ESR or BSR(FR).+(?s:(?=.*(ESR))) | |
| (?s)(?-i:BSR|(?!\A)\G).*?\KFR(?!ESR) | |
| (?s)(?-i:BSR|(?!\A)\G)(?s-i:(?!ESR).)*?\K(?-si:FR) | |
| (?s)(?-i:BSR|(?!\A)\G).*?\K(?-si:FR)(?=(?s-i:.*?ESR)) | |
| (?s)BSR(FR)(?-s).+ESR or BSR(FR).+(?s:(?=.*(ESR))) | |
| (?-si:BSR|(?!\A)\G).*?\KFR(?!ESR) | |
| (?-si:BSR|(?!\A)\G)(?s-i:(?!ESR).)*?\K(?-si:FR) | |
| (?-si:BSR|(?!\A)\G).*?\K(?-si:FR)(?=(?s-i:.*?ESR)) | |
| (?s)BSR(FR)(?-s).+ESR sau BSR(FR).+(?s:(?=.*(ESR))) | |
| (?-s)(?-i:BSR|(?!\A)\G).*?\K(?-i:FR)(?=ESR) | |
| (?-si:BSR|(?!\A)\G)(?s-i:(?!ESR).)*?\K(FR)+ REPLACE BY: \2 sau \3 | |
| (?-i:BSR|\G(?!^))(?s:(?!ESR).)*?\K(?-i:FR) | |
| BSR(?!\A)(?-s:.*?)\K(?-si:FR)(?=(?s-i:.*?ESR)) | |
| BSR\K.*?(?!\A)(?-s:.*?)\K(?-si:FR)(?=(?s-i:.*?ESR)) | |
| (?-i:BSR|(?!\A)\G)(?s:(?!ESR).)*?\K(?-i:FR) | |
| (?-i:BSR|(?!^)\G)(?s:(?!ESR).)*?\K(?-i:FR) | |
| (?s)\A.*BSR(FR)*ESR.*\z|\A.*BSR(FR)+ESR.*\z | |
| (?s)\A.*BSR(FR)+ESR.*\z | |
| (?s)\A.*BSR+(FR)+ESR.*\z | |
| (?:BSR|\G)\S*\K(?:FR)(?=.*ESR) | |
| (?:BSR|\G)\S*\K(FR)(?=.*ESR) | |
| (?:BSR|\G)\S*\K(?:\h+FR)(?=.*ESR) | |
| (?<!=\x20)(?<!=)FR(?!>|ESR|\x20>|\x20/>|\?>|\x20\?>|BSR) | |
| (?<!=\x20)(?<!=)FR(?!>|ESR|\x20>|\x20/>|\?>|\x20\?>|\x20BSR) | |
| (?<!=\x20)(?<!=)FR(?!>|ESR|\x20\?>|\x20BSR) | |
| (?-is)^(?!.*BSR)(?!.*ESR)(?:(FR)) | |
| (?-is)^(?!.*BSR)(?!.*ESR)(?:(FR)(.+)) | |
| (BSR).*\K(?:\h+(FR))(?=.*ESR) | |
| (?-i:BSR|\G(?!^))(?s:(ESR).)*?\K(?-i:FR) | |
| (BSR)+(.)+\K(FR)(?s:(?=.*(ESR))) | |
| (BSR)(FR)(.*?)(ESR) | |
| (?s-i)^.+BSR\R|ESR.+|(?-s)^(FR)|(.+))$ | |
| (?s-i)^.+BSR\R|ESR.+|(?-s)^(FR)(.+))$ | |
| (BSR)(.*?)(FR)(.*?)(ESR) | |
| (?s)(?-i:(BSR).+?ESR|\G(?!^))((?!(ESR)).)*?\K(FR) | |
| (?s)(?-i:BSR.+?ESR|\G(?!^))((?!ESR).)*?\KFR | |
| (BSR)+(.)+\K(FR)(?=.*ESR) | |
| (BSR)+(.)+\K(FR)(?=.*)(ESR) | |
| (BSR)*\K(?:(FR))(?=.*ESR) | |
| (BSR)\K(?-s)(.*?)\KFR(?=.*ESR) | |
| .*?(BSR)(?-s)(.*?)(FR).*(.*ESR) | |
| (BSR).*(\b(FR)\b.*).*(ESR) | |
| (?:BSR|\G).*?\K(?:FR(*SKIP)(*FAIL)|<(?!ESR)[^>]*>) | |
| BSR(?=(?:(?!ESR).)*?FR).+?ESR\R? | |
| BSR(?=(?:(?!FR).)*?FR).+?ESR\R? | |
| BSR(?=(?:(?!ESR).)*?FR)(?=(?:(?!ESR).)*?FR).+?ESR\R? | |
| BSR(?=(?:(?!ESR).)*?FR)(?=(?:(?!ESR).)).+?ESR\R? | |
| (?s)(?:BSR|\G)(?:(?!ESR).)*?\K(FR) | |
| (?s)(?:BSR|\G(?!^))(?:(?!ESR).)*?\KFR | |
| (?!BSR)*\K(?!BSR)(?:(FR)))(?=.*ESR) | |
| (?s)(?:BSR|\G)(?:(?!ESR).)*?\K(?:FR) | |
| (?s)(?:BSR|(?!\A)\G)(?:(?!ESR).)*?\KFR | |
| (?s)(?:BSR|\G).*?\K(?:FR(*SKIP)(*FAIL)|<(?:(?!ESR).)+?>) | |
| (?:BSR|\G).*?\K(?:FR(*SKIP)(*FAIL)|<(?:(?!ESR).)+?>) | |
| (?:BSR|\G).*?\K).*?(FR)(?=.*ESR) | |
| (?:BSR|\G).*?\K)(FR).*?(?=.*ESR) | |
| (?:BSR|\G)(FR).*?\K).*?(?=.*ESR) | |
| (?s-i)(?:BSR|\G)(?:(?!ESR).)*?\K(FR) | |
| (?:BSR|\G)(FR).*?(FR)\K).*?(?=.*ESR) | |
| (?s-i)(?:<p[^>]*>|(?!\A)\G)(?:(?!</p>).)*?\K<(?!(?:/[ap]>|a\x20[^>]+>))[^>]*> | |
| (?s-i)(?:BSR|(?!\A)\G)(?:(?!ESR).)*?\K<(?!(?:FR1|FR2))[^>]*> | |
| (?:BSR|\G).*?\K(?:FR(*SKIP)(*FAIL)|<(?:(?!ESR).)+?>) | |
| (?:BSR|\G).*?\K(?:FR(*SKIP)(*FAIL)|<(?:(?!ESR1|ESR2).)+?>) | |
| BSR(?:[^<]+|<(?!FR1|FR2))?ESR(*SKIP)(FAIL)|<(?!FR1|FR2)[^>]+> | |
| SPECIFICATII: | |
| Let BSR ( Begin Search-region Regex ) be the regex which defines the beginning of the area where the search for FR, must start | |
| Let ESR ( End Search-region Regex) be the regex which defines, implicitly, the area where the search for FR, must end | |
| Let FR (Find Regex ) be the regex which defines the char, string or expression to be searched | |
| Let RR (Replacement Regex ) be the regex which defines the char, string or expression which must replace the FR expression. REPLACE RR (means can be \1 \2 ...or $1 $2 or other | |
| 1. Componente cheie: | |
| - BSR (Begin Search-region Regex): Marchează începutul zonei de căutare | |
| - ESR (End Search-region Regex): Marchează sfârșitul zonei de căutare | |
| - FR (Find Regex): Expresia de căutat în zona definită | |
| - RR (Replacement Regex): Expresia cu care se înlocuiește FR (dacă e cazul) | |
| 2. Structura generală a expresiei regulate: | |
| (?s)(?:BSR|(?!\A)\G)(?:(?!ESR).)*?\K(?:FR) | |
| 3. Pași de aplicare: | |
| a. Identifică BSR și ESR în text | |
| b. Definește FR (ce trebuie găsit) și RR (cu ce se înlocuiește, dacă e cazul) | |
| c. Construiește expresia regulată folosind structura generală | |
| d. Aplică în tool-ul de editare (ex: Notepad++) folosind "Find and Replace" | |
| 4. Componente cheie ale expresiei: | |
| - (?s): Activează modul dotall pentru potriviri multi-linie | |
| - (?!\A)\G: Continuă căutarea de la ultima potrivire | |
| - (?:(?!ESR).)*?: Caută orice până la ESR (non-greedy) | |
| - \K: Resetează începutul potrivirii | |
| 5. Adaptare pentru cazuri specifice: | |
| - Ajustează BSR, ESR, FR și RR în funcție de necesitățile textului și sarcinii | |
| - Modifică expresia pentru a include sau exclude anumite elemente după caz | |
| In ceea ce priveste Python, aceste formule pot f integrate doar daca folosesti libraria https://pypi.org/project/regex/ , fiindca aceasta librarie are secvente precum \K sau altele, pe care libraria "import re" nu le are integrate | |
| -------------------------------------------------------------------------------- | |
| ^(.*?)(On\s+[A-Za-z]+\s+\d{1,2},\s+\d{4})(.*)$ | |
| ↑ ↑ ↑ | |
| BSR FR ESR | |
| FIND: BSRFRESR Replace: RR | |
| BSR (^(.*?)) stabilește unde începe căutarea (de la începutul textului). | |
| FR ((On\s+[A-Za-z]+\s+\d{1,2},\s+\d{4})) identifică ce vrem să găsim. | |
| ESR ((.*)$) delimitează unde se termină căutarea (până la sfârșitul textului). | |
| RR ($2) specifică ce păstrăm în rezultat (doar FR). | |
| Exemplu aici: | |
| https://archive.org/details/regex-rezolvare-caz-cu-generic-formulas | |
| https://superuser.com/questions/1793173/regex-find-the-string-between-2-strings | |
| https://community.notepad-plus-plus.org/topic/22690/generic-regex-replacing-in-a-specific-zone-of-text | |
| https://superuser.com/questions/1793173/regex-find-the-string-between-2-strings | |
| https://gist.github.com/me-suzy/07a4f6ac1ee9fddf420f9bb4f4456282 | |
| https://community.notepad-plus-plus.org/topic/22595/regex-how-to-get-off-the-connecting-line-from-the-title-of-a-hyperlink | |
| Exemplu: | |
| <table width="648" border="0"> | |
| <tr> | |
| <td><h1 class="den_articol" itemprop="name">Memoria Harenae</h1></td> | |
| </tr> | |
| <tr> | |
| <td class="text_dreapta">On Martie 18, 2025, in <a href="https://neculaifantanaru.com/leadership-impact.html" title="Vezi toate articolele din Leadership Impact" class="external" rel="category tag">Leadership Impact</a>, by Neculai Fantanaru</td> | |
| </tr> | |
| </table> | |
| Rezultat: On Martie 18, 2025 FIND: <td class="text_dreapta">(.*?)(, in.*)$ REplace by: \1 | |
| BSR = <tr> | |
| ESR = </tr> | |
| FR = <td class="text_dreapta">(.*?)(, in.*)$ | |
| RR = 1 | |
| ------------- EXEMPLU REZOLVAT CU libraria REGEX (solutia 1): | |
| import regex | |
| html_block = """ | |
| <table width="648" border="0"> | |
| <tr> | |
| <td><h1 class="den_articol" itemprop="name">Memoria Harenae</h1></td> | |
| </tr> | |
| <tr> | |
| <td class="text_dreapta">On Martie 18, 2025, in <a href="https://neculaifantanaru.com/leadership-impact.html" title="Vezi toate articolele din Leadership Impact" class="external" rel="category tag">Leadership Impact</a>, by Neculai Fantanaru</td> | |
| </tr> | |
| </table> | |
| """ | |
| # Defining the components according to the generic formula | |
| BSR = r'<td class="text_dreapta">' # Begin Search Region | |
| ESR = r',\s+in\s+<a href="' # End Search Region | |
| FR = r'On\s+\w+\s+\d{1,2},\s+\d{4}' # Find Regex (the date pattern) | |
| # Using the generic formula from your file | |
| pattern = fr'(?-si:{BSR}|(?!\A)\G)(?s-i:(?!{ESR}).)*?\K(?-si:{FR})' | |
| match = regex.search(pattern, html_block) | |
| if match: | |
| extracted_date = match.group() | |
| print(extracted_date) # Output: On Martie 18, 2025 | |
| else: | |
| print("Nu a fost găsită nicio potrivire.") | |
| ------------- EXEMPLU REZOLVAT CU libraria REGEX (solutia 2): | |
| import regex | |
| html_block = """ | |
| <table width="648" border="0"> | |
| <tr> | |
| <td><h1 class="den_articol" itemprop="name">Memoria Harenae</h1></td> | |
| </tr> | |
| <tr> | |
| <td class="text_dreapta">On Martie 18, 2025, in <a href="https://neculaifantanaru.com/leadership-impact.html" title="Vezi toate articolele din Leadership Impact" class="external" rel="category tag">Leadership Impact</a>, by Neculai Fantanaru</td> | |
| </tr> | |
| </table> | |
| """ | |
| # Componente conforme cu formulele generice din fișierul tău | |
| BSR = r'<td class="text_dreapta">' # Începutul zonei de căutare | |
| ESR = r',\s+in\s+<a href="' # Sfârșitul zonei de căutare | |
| FR = r'On\s+\w+\s+\d{1,2},\s+\d{4}' # Modelul datei de extras | |
| # Folosim formula generică PARSING 7 din fișier | |
| pattern = fr'(?-si:{BSR}|(?!\A)\G)(?s-i:(?!{ESR}).)*?\K(?-si:{FR})' | |
| # Căutare cu opțiuni: | |
| # - Overlapped=True pentru a gestiona potrivirile suprapuse | |
| # - DOTALL pentru a include newline-uri în căutare | |
| match = regex.search(pattern, html_block, regex.DOTALL) | |
| if match: | |
| print("Data extrasă:", match.group()) # On Martie 18, 2025 | |
| else: | |
| print("Nu s-a găsit nicio potrivire.") | |
| # Alternativă folosind o altă formulă generică din fișier: | |
| # (?-s)(?-i:BSR|(?!\A)\G).*?\K(?-i:FR)(?=ESR) | |
| alt_pattern = fr'(?-s)(?-i:{BSR}|(?!\A)\G).*?\K(?-i:{FR})(?={ESR})' | |
| alt_match = regex.search(alt_pattern, html_block) | |
| if alt_match: | |
| print("Verificare alternativă:", alt_match.group()) | |
| --------------- | |
| Exemplu: | |
| (?-si:BSR|(?!\A)\G)(?s-i:(?!ESR).)*?\KFR | |
| este inlocuit cu datele problemei astfel: | |
| FIND: (?-si:<tr>|(?!\A)\G)(?s-i:(?!</tr>).)*?\K<td class="text_dreapta">(.*?)(, in.*)$ | |
| REPLACE BY: \1 | |
| -------- | |
| EXTRAGE DATA On Martie 18, 2025 din textul: | |
| import re | |
| html_block = """ | |
| <table width="648" border="0"> | |
| <tr> | |
| <td><h1 class="den_articol" itemprop="name">Memoria Harenae</h1></td> | |
| </tr> | |
| <tr> | |
| <td class="text_dreapta">On Martie 18, 2025, in <a href="https://neculaifantanaru.com/leadership-impact.html" title="Vezi toate articolele din Leadership Impact" class="external" rel="category tag">Leadership Impact</a>, by Neculai Fantanaru</td> | |
| </tr> | |
| </table> | |
| """ | |
| # Regex-ul folosit: | |
| # - Lookbehind pentru a ne asigura că potrivirea începe după tag-ul <td class="text_dreapta"> | |
| # - Grupul 1 capturează textul dorit "On Martie 18, 2025" | |
| # - Lookahead pentru a verifica că imediat după apare ", in <a href=" ... | |
| pattern = r'(?<=<td class="text_dreapta">)(On\s+\w+\s+\d{1,2},\s+\d{4})(?=,\s+in\s+<a href=")' | |
| match = re.search(pattern, html_block, flags=re.DOTALL) | |
| if match: | |
| extracted_date = match.group(1) | |
| print(extracted_date) | |
| else: | |
| print("Nu a fost găsită nicio potrivire.") | |
| ----------- |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment