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
No Match

r"
"
gm

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"(^[A-Z][a-z][^\d:]*$)\n(\s+)?((([A-Za-z ,]+)|(.*)?(\(\w+.*\)))(\s+)?((-[\d,]+|[\d,]+)\s(\s+)?(-[\d,]+|[\d,]+)(.*)?))" test_str = ("56 Frontken Corporation Berhad -651020-T \n" "ANNUAL REPORT 2018 \n" "Statements Of Cash Flows \n" "For The Financial Year Ended 31 December 2018 \n" "The Group The Company \n" "2018 2017 2018 2017 \n" "RM RM RM RM \n" "CASH FLOWS FROM/(FOR) \n" "OPERATING ACTIVITIES \n" "Profit before tax 75,615,413 46,147,364 17,519,528 13,008,793 \n" "Adjustments for: \n" "Depreciation of property, plant and \n" " equipment 17,300,505 18,471,479 17,331 21,667 \n" "Interest expense 568,006 931,264 56,109 194,461 \n" "Allowance for impairment loss on \n" "property, plant and equipment 0 49,749 0 0 \n" "Unrealised (gain)/loss on foreign exchange -616,303 4,613,657 190,487 95,813 \n" "Allowance for impairment losses on \n" "amount owing by a subsidiary 0 0 1,242,999 270,000 \n" "Allowance for impairment losses on \n" " receivables 932,838 71,151 0 0 \n" "Bad debt written off 153,130 0 0 0 \n" "Inventories written off 0 137,087 0 0 \n" "Gain on disposal of an associate -672,207 0 0 0 \n" "Property, plant and equipment \n" "written off 10,678 506,875 0 0 \n" "Share of results in an associate -78,115 51,126 0 0 \n" "Interest income -917,220 -844,780 -549,987 -514,753 \n" "Gain on disposal of property, plant \n" "and equipment -164,099 -11,181 0 0 \n" "Writeback of allowance for impairment \n" "losses on trade receivables Dividend -10,000 -60,137 0 0 \n" "income from subsidiaries 0 0 -23,527,968 -17,028,204 \n" "Operating Profit/(Loss) Before \n" "Working Capital Changes 92,122,626 70,063,654 -5,051,501 -3,952,223 \n" "(Increase)/Decrease in: \n" " Inventories -379,713 -2,955,410 0 0 \n" "Trade receivables -16,590,199 2,180,666 0 0 \n" "Other receivables and prepaid expenses -1,492,450 -424,370 -27,867 13,997 \n" "Amount owing by an associate 1,330,780 1,698 0 0 \n" "(Decrease)/Increase in: \n" "Trade payables -826,277 -1,354,959 0 0 \n" "Other payables and accrued expenses 3,376,361 9,419,139 1,190,933 956,353 \n" "Cash Generated From/(For) Operations 77,541,128 76,930,418 -3,888,435 -2,981,873 \n" "Taxes paid -14,218,858 -7,901,643 0 0 \n" "Net Cash From/(For) Operating Activities 63,322,270 69,028,775 -3,888,435 -2,981,873 \n" "The accompanying Notes form an integral part of these Financial Statements. \n" " \n" "Frontken Corporation Berhad -651020-T 57 \n" "ANNUAL REPORT 2018 \n" "Statements Of Cash Flows \n" "For The Financial Year Ended 31 December 2018 (cont’d) \n" "The Group The Company \n" "2018 2017 2018 2017 \n" "RM RM RM RM \n" "CASH FLOWS (FOR)/FROM INVESTING \n" "ACTIVITIES \n" "Repayment from subsidiaries 0 0 1,514,464 9,000,836 \n" "Purchase of property, plant and equipment -7,492,297 -20,152,154 -3,689 -830 \n" "Dividend received from subsidiaries 0 0 21,039,335 14,800,304 \n" "Additional investment/acquisition of \n" "subsidiaries (Note 11) -7,133,082 -13,273,927 -7,133,082 -13,273,927 \n" "Proceeds from disposal of an associate 2,496,557 0 0 0 \n" "Proceeds from disposal of property, plant \n" "and equipment 1,077,256 138,530 0 0 \n" "Withdrawal of short-term investments 0 2,197,876 0 0 \n" "Net withdrawal/(placement) of fixed \n" "deposits with licensed banks 2,992,578 -1,833,200 1,104,683 -31,986 \n" "Interest received 917,220 844,780 549,987 514,753 \n" "Net Cash (For)/From Investing Activities -7,141,768 -32,078,095 17,071,698 11,009,150 \n" "CASH FLOWS FOR FINANCING ACTIVITIES \n" "Decrease in amount owing to subsidiaries \n" "(Note 29(a)) 0 0 591,745 -5,754,789 \n" "Repayment of term loans (Note 29(a)) -16,712,117 -7,883,738 -2,037,933 -2,090,577 \n" "Interest paid -568,006 -931,264 -56,109 -194,461 \n" "Dividend paid by the Company (Note 26) -7,335,779 -5,239,843 -7,335,779 -5,239,843 \n" "Dividend paid by a subsidiary to non- \n" "controlling interests -2,626,027 -1,984,852 0 0 \n" "Drawdown of term loans (Note 29(a)) 0 7,901,920 0 0 \n" "Payment of hire purchase payables \n" "(Note 29(a)) -544,511 -251,063 0 0 \n" "Net Cash For Financing Activities -27,786,440 -8,388,840 -8,838,076 -13,279,670 \n" "NET INCREASE/(DECREASE) IN \n" "CASH AND CASH EQUIVALENTS 28,394,062 28,561,840 4,345,187 -5,252,393 \n" "Effect of exchange rate changes 293,086 -6,376,820 8,004 -374,697 \n" "CASH AND CASH EQUIVALENTS AT \n" "BEGINNING OF YEAR 120,252,919 98,067,899 9,757,017 15,384,107 \n" "CASH AND CASH EQUIVALENTS AT \n" "END OF YEAR (Note 29(b)) 148,940,067 120,252,919 14,110,208 9,757,017 \n" "Note : During the financial year, the Group and the Company acquired property, plant and equipment at an aggregate cost of \n" "RM9,892,297 and RM3,689 -2017: RM20,152,154 and RM830, respectively, of which RM2,400,000 and NIL -2017: NIL and NIL, \n" "respectively, was acquired under hire-purchase arrangements. \n" "The accompanying Notes form an integral part of these Financial Statements. ") matches = re.finditer(regex, test_str, re.MULTILINE) 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