Regular Expressions 101

Save & Share

Flavor

  • PCRE2 (PHP >=7.3)
  • PCRE (PHP <7.3)
  • ECMAScript (JavaScript)
  • Python
  • Golang
  • Java 8
  • .NET 7.0 (C#)
  • Rust
  • Regex Flavor Guide

Function

  • Match
  • Substitution
  • List
  • Unit Tests

Tools

Sponsors
There are currently no sponsors. Become a sponsor today!
An explanation of your regex will be automatically generated as you type.
Detailed match information will be displayed here automatically.
  • All Tokens
  • Common Tokens
  • General Tokens
  • Anchors
  • Meta Sequences
  • Quantifiers
  • Group Constructs
  • Character Classes
  • Flags/Modifiers
  • Substitution
  • A single character of: a, b or c
    [abc]
  • A character except: a, b or c
    [^abc]
  • A character in the range: a-z
    [a-z]
  • A character not in the range: a-z
    [^a-z]
  • A character in the range: a-z or A-Z
    [a-zA-Z]
  • Any single character
    .
  • Alternate - match either a or b
    a|b
  • Any whitespace character
    \s
  • Any non-whitespace character
    \S
  • Any digit
    \d
  • Any non-digit
    \D
  • Any word character
    \w
  • Any non-word character
    \W
  • Non-capturing group
    (?:...)
  • Capturing group
    (...)
  • Zero or one of a
    a?
  • Zero or more of a
    a*
  • One or more of a
    a+
  • Exactly 3 of a
    a{3}
  • 3 or more of a
    a{3,}
  • Between 3 and 6 of a
    a{3,6}
  • Start of string
    ^
  • End of string
    $
  • A word boundary
    \b
  • Non-word boundary
    \B

Regular Expression

/
/
gmu

Test String

Code Generator

Generated Code

# coding=utf8 # the above tag defines encoding for this document and is for Python 2.x compatibility import re regex = r"(?i)PODATNIK\:[\s\a]*(?<NAZWA>[\S ]*)(?<KODPOCZTOWY>\d{2}-\d{3} )(?<LOKALIZACJA>[a-ż ]+)[\s\a]*ORYGINAŁ[\s\a]*\k<LOKALIZACJA>,[\s]*(?<DATA>\d{4}-\d{2}-\d{2})[\s\a]*(?<ULICA>[a-ż 0-9]*)[\s\a]*Bank[\s]*:[\s]*(?<BANK>[\w ]*)[\s\a]*NIP[\s]*:[\s]*(?<NIP>\d{3}-\d{3}-\d{3})[\s\a]*Konto[\s]*:[\s]*(?<KONTO>\d{26})[\s\a\S]*FAKTURA VAT[\s]*(?<NRFAKTURY>\d{2}\/\d{2}\/\d{4})*[\s\a]*Data wystawienia[\s\:]*(?<DATAWYSTAWIENIA>\d{4}-\d{2}-\d{2})[\s]*Data sprzedaży[\s\:]*(?<DATASPRZEDAZY>\d{4}-\d{2}-\d{2})[\s\a]*Na podstawie[\s\:]*(?<NAPODTSAWIE>[a-ż]\d{5}),[\s\a]*z dnia[\s]*:[\s]*(?<ZDNIA>\d{4}[-]?\d{2}[-]?\d{2})[\s\a]*NABYWCA[\s]*:[\s]*(?<NABYWCA>[\S ]*)[\s\a]*\(811\)[\s]*:[\s]*(?<ULICA2>[a-ż 0-9]*\d[a-z]{1,3} )(?<MIASTO>[a-ż]*)[\s\a]*,[\s\a]*(?<KODPOCZTOWY2>\d{2}[-]?\d{3})[\s\a]*N2P[\s]*:[\s]*(?<N2P>\d{3}[-]?\d{3}[-]?\d{2}[-]?\d{2})[\s\a]*Operator prowadzący[\s]*:[\s]*(?<OPERATOR>[a-ż ]*),[\s]*Tel.[\s]*:[\s]*(?<TEL>[0-9]{9}|[0-9 ]*)" test_str = ("PODATNIK:\n" "Rogum Kable Sp. z o.o. 83-000 Pruszcz Gdański\n" "ORYGINAŁ\n" "Pruszcz Gdański, 2020-04-28\n" "\n" "Gruńwaldzka 66\n" "\n" "Bank: Bank Polska Kasa Opieki SA\n" "\n" "NIP : 000-111-111\n" "\n" "Konto: 57124012681111001033385998\n" "\n" "/\n" "FAKTURA VAT 07/04/2020\n" "Data wystawienia : 2020-04-28 Data sprzedaży : 2020-04-28\n" " \n" "Na podstawie : T59999, z dnia: 2019-12-28\n" " \n" "NABYWCA : Pojazdy Szynowe PESA Bydgoszcz S.A.\n" "(811) : Zygmunta Augusta 11avb Bydgoszcz, 85-082 \n" "N2P : 554-031-17-75\n" "Operator prowadzący : Łukasz Czajkowski, Tel.: 506234233, E-mail: l.czajkowski@rogum.com.pl\n" "/\n" "Poprzednie zaliczki/zadatki: Fzal 54/04/2020 2020-04-06 (246 000,00)\n" "Lp\n" "Nazwa pełna\n" "Ilość\n" "Jm\n" "Cena\n" "Wartość\n" "Stawka\n" "Wartość\n" "Wartość\n" "Indeks (K)\n" "\n" "\n" "\n" "\n" "\n" "NETTO PL\n" "NETTO PLN\n" "VAT\n" "VAT PLN\n" "BRUTTO PLN\n" "\n" "\n" "1\n" "LiHCH 0,6/1kV 3x1,5\n" "200\n" "m\n" "2,70\n" "540,00\n" "23%\n" "124,20\n" "664,20\n" "9124-1100-000-075\n" "\n" "2\n" "LiHCH 0,6/1kV 3x1,5\n" "86\n" "m\n" "2,70\n" "232,20\n" "23%\n" "53,41\n" "285,61\n" "9124-1100-000-075\n" "\n" "PRZED ODLICZ. ZAL./ZAD. - Zestawienie podatku VAT\n" "Netto\n" "Stawka\n" "VAT\n" "Brutto\n" "\n" " \n" "772,20\n" "23%\n" "177,61\n" "949,81\n" "\n" "\n" "\n" "\n" "\n" "\n" "\n" "ODLICZENIE ZAL./ZAD. - Zestawienie podatku VAT\n" "Netto\n" "Stawka\n" "VAT\n" "Brutto\n" "\n" " \n" "-772,20\n" "23%\n" "-177,61\n" "-949,81\n" "\n" " \n" "Zestawienie podatku VAT\n" "Netto\n" "\n" "Stawka\n" "VAT\n" "\n" "Brutto\n" "\n" "\n" "\n" "\n" "\n" "\n" "\n" "\n" "\n" "RAZEM (2 Poz.)\n" "\n" "0,00\n" " \n" "\n" "0,00\n" "0,00\n" "\n" "SUMA OGÓŁEM\n" "\n" "\n" "\n" "\n" "\n" "0,00\n" "\n" "\n" " \n" "Słownie : zero złotych, zero groszy\n" " \n" "DO ZAPŁATY : 0,00 zł Rozliczono\n" " \n" " \n" " \n" " \n" " \n" "Sara Żołnowska\n" " \n" " . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . \n" "podpis osoby upoważnionej pieczęć i podpis wystawiającego do odbioru Faktury VAT\n" "Druk Streamsoft PRESTIŻ lic. 5529 v. 7.0.347.168 Str. 1 z 1") matches = re.finditer(regex, test_str, re.MULTILINE | re.UNICODE) for matchNum, match in enumerate(matches, start=1): print ("Match {matchNum} was found at {start}-{end}: {match}".format(matchNum = matchNum, start = match.start(), end = match.end(), match = match.group())) for groupNum in range(0, len(match.groups())): groupNum = groupNum + 1 print ("Group {groupNum} found at {start}-{end}: {group}".format(groupNum = groupNum, start = match.start(groupNum), end = match.end(groupNum), group = match.group(groupNum))) # Note: for Python 2.7 compatibility, use ur"" to prefix the regex and u"" to prefix the test string and substitution.

Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Python, please visit: https://docs.python.org/3/library/re.html