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

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

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 Swift 5.2, please visit: https://developer.apple.com/documentation/foundation/nsregularexpression