Regular Expressions 101

Save & Manage Regex

  • Current Version: 2
  • Save & Share
  • Community Library

Flavor

  • PCRE2 (PHP)
  • ECMAScript (JavaScript)
  • Python
  • Golang
  • Java
  • .NET 7.0 (C#)
  • Rust
  • PCRE (Legacy)
  • Regex Flavor Guide

Function

  • Match
  • Substitution
  • List
  • Unit Tests
Sponsors
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
Processing...

Test String

Code Generator

Generated Code

import re regex = re.compile(r"(\w+)(\t)(.*)(\b)") test_str = ("trade asian exporters fear damage from u s japan rift mounting trade friction between the u s and japan has raised fears among many of asia s exporting nations that the row could inflict far reaching economic damage businessmen and officials said they told reuter correspondents in asian capitals a u s move against japan might boost protectionist sentiment in the u s and lead to curbs on american imports of their products but some exporters said that while the conflict would hurt them in the long run in the short term tokyo s loss might be their gain the u s has said it will impose mln dlrs of tariffs on imports of japanese electronics goods on april in retaliation for japan s alleged failure to stick to a pact not to sell semiconductors on world markets at below cost unofficial japanese estimates put the impact of the tariffs at billion dlrs and spokesmen for major electronics firms said they would virtually halt exports of products hit by the new taxes we wouldn t be able to do business said a spokesman for leading japanese electronics firm matsushita electric industrial co ltd mc t if the tariffs remain in place for any length of time beyond a few months it will mean the complete erosion of exports of goods subject to tariffs to the u s said tom murtha a stock analyst at the tokyo office of broker james capel and co in taiwan businessmen and officials are also worried we are aware of the seriousness of the u s threat against japan because it serves as a warning to us said a senior taiwanese trade official who asked not to be named taiwan had a trade trade surplus of billion dlrs last year pct of it with the u s the surplus helped swell taiwan s foreign exchange reserves to billion dlrs among the world s largest we must quickly open our markets remove trade barriers and cut import tariffs to allow imports of u s products if we want to defuse problems from possible u s retaliation said paul sheen chairman of textile exporters taiwan safe group a senior official of south korea s trade promotion association said the trade dispute between the u s and japan might also lead to pressure on south korea whose chief exports are similar to those of japan last year south korea had a trade surplus of billion dlrs with the u s up from billion dlrs in in malaysia trade officers and businessmen said tough curbs against japan might allow hard hit producers of semiconductors in third countries to expand their sales to the u s in hong kong where newspapers have alleged japan has been selling below cost semiconductors some electronics manufacturers share that view but other businessmen said such a short term commercial advantage would be outweighed by further u s pressure to block imports that is a very short term view said lawrence mills director general of the federation of hong kong industry if the whole purpose is to prevent imports one day it will be extended to other sources much more serious for hong kong is the disadvantage of action restraining trade he said the u s last year was hong kong s biggest export market accounting for over pct of domestically produced exports the australian government is awaiting the outcome of trade talks between the u s and japan with interest and concern industry minister john button said in canberra last friday this kind of deterioration in trade relations between two countries which are major trading partners of ours is a very serious matter button said he said australia s concerns centred on coal and beef australia s two largest exports to japan and also significant u s exports to that country meanwhile u s japanese diplomatic manoeuvres to solve the trade stand off continue japan s ruling liberal democratic party yesterday outlined a package of economic measures to boost the japanese economy the measures proposed include a large supplementary budget and record public works spending in the first half of the financial year they also call for stepped up spending as an emergency measure to stimulate the economy despite prime minister yasuhiro nakasone s avowed fiscal reform program deputy u s trade representative michael smith and makoto kuroda japan s deputy minister of international trade and industry miti are due to meet in washington this week in an effort to end the dispute reuter \n" "grain china daily says vermin eat pct grain stocks a survey of provinces and seven cities showed vermin consume between seven and pct of china s grain stocks the china daily said it also said that each year mln tonnes or pct of china s fruit output are left to rot and mln tonnes or up to pct of its vegetables the paper blamed the waste on inadequate storage and bad preservation methods it said the government had launched a national programme to reduce waste calling for improved technology in storage and preservation and greater production of additives the paper gave no further details reuter \n" "ship australian foreign ship ban ends but nsw ports hit tug crews in new south wales nsw victoria and western australia yesterday lifted their ban on foreign flag ships carrying containers but nsw ports are still being disrupted by a separate dispute shipping sources said the ban imposed a week ago over a pay claim had prevented the movement in or out of port of nearly vessels they said the pay dispute went before a hearing of the arbitration commission today meanwhile disruption began today to cargo handling in the ports of sydney newcastle and port kembla they said the industrial action at the nsw ports is part of the week of action called by the nsw trades and labour council to protest changes to the state s workers compensation laws the shipping sources said the various port unions appear to be taking it in turn to work for a short time at the start of each shift and then to walk off cargo handling in the ports has been disrupted with container movements most affected but has not stopped altogether they said they said they could not say how long the disruption will go on and what effect it will have on shipping movements reuter \n" "acq sumitomo bank aims at quick recovery from merger sumitomo bank ltd sumi t is certain to lose its status as japan s most profitable bank as a result of its merger with the heiwa sogo bank financial analysts said osaka based sumitomo with desposits of around trillion yen merged with heiwa sogo a small struggling bank with an estimated billion dlrs in unrecoverable loans in october but despite the link up sumitomo president koh komatsu told reuters he is confident his bank can quickly regain its position we ll be back in position in first place within three years komatsu said in an interview he said that while the merger will initially reduce sumitomo s profitability and efficiency it will vastly expand sumitomo s branch network in the tokyo metropolitan area where it has been relatively weak but financial analysts are divided on whether and how quickly the gamble will pay off some said sumitomo may have paid too much for heiwa sogo in view of the smaller bank s large debts others argue the merger was more cost effective than creating a comparable branch network from scratch the analysts agreed the bank was aggressive it has expanded overseas entered the lucrative securities business and geared up for domestic competition but they questioned the wisdom of some of those moves they ve made bold moves to put everything in place now it s largely out of their hands said kleinwort benson ltd financial analyst simon smithson among sumitomo s problems are limits placed on its move to enter u s securities business by taking a share in american investment bank goldman sachs and co sumitomo last august agreed to pay mln dlrs for a pct limited partnership in the bank but for the time being at least the federal reserve board has forbidden them to exchange personnel or increase the business they do with each other the tie up is widely looked on as a lame duck because the fed was stricter than sumitomo expected said one analyst but komatsu said the move will pay off in time u s regulations will change in the near future and if so we can do various things we only have to wait two or three years not until the st century komatsu said komatsu is also willing to be patient about possible routes into the securities business at home article of the securities and exchange act japan s version of the u s glass steagall act separates commercial from investment banking but the walls between the two are crumbling and komatsu said he hopes further deregulation will create new opportunities we need to find new business chances komatsu said in some cases these will be securities related in some cases trust bank related that s the kind of deregulation we want until such changes occur sumitomo will focus on such domestic securities business as profitable government bond dealing and strengthening relations with meiko securities co ltd in which it holds a five pct share komatsu said he said sumitomo is cautiously optimistic about entering the securities business here through its swiss universal bank subsidiary banca del gottardo the finance ministry is expected to grant licences to securities subsidiaries of u s commercial banks soon following a similar decision for subsidiaries of european universal banks in which the parent holds a less than pct but komatsu is reluctant to push hard for a similar decision on a gottardo subsidiary we don t want to make waves we expect this will be allowed in two or three years he said like other city banks sumitomo is also pushing to expand lending to individuals and small and medium businesses to replace disappearing demand from big business he added the analysts said sumitomo will have to devote a lot of time to digesting its most recent initiatives including the merger with ailing heiwa sogo it s sumitomo been bold in its strategies said kleinwort s smithson after that it s a question of absorbing and juggling around it will be the next decade before we see if the strategy is right or wrong reuter ") matches = regex.finditer(test_str) for match_num, match in enumerate(matches, start=1): print(f"Match {match_num} was found at {match.start()}-{match.end()}: {match.group()}") for group_num, group in enumerate(match.groups(), start=1): print(f"Group {group_num} found at {match.start(group_num)}-{match.end(group_num)}: {group}")

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