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

/
/
gmx

Test String

Code Generator

Generated Code

import java.util.regex.Matcher; import java.util.regex.Pattern; public class Example { public static void main(String[] args) { final String regex = "\\{((?:[^\\{\\}]|(?R))*)\\}"; final String string = "1. e4 g6 2. d4 Bg7 3. c3 d5 ({} 3... c5 {is not appropriate, and after playing through the game\n" + "I hope you will appreciate why that is so - I certainly did. --- Some\n" + "background to this game will be of interest. Liverpool 2008 was my first\n" + "venture into the European Union Championship and I was particularly keen to\n" + "prepare well for each opponent. I quickly realized that Tiviakov is an\n" + "excellent opening theoretician and rarely does any player of the Black pieces\n" + "get complete equality against him, let alone any advantage. --- Using the\n" + "Sniper, however, you will see that I managed to outplay him in the opening\n" + "phase of the game, with a variation that I consider to be my most important\n" + "contribution to opening theory:}) 4. Nd2 c5 {The move order of 1...g6, 2...Bg7\n" + "and 3...c5 has not been played, but we still see the main character of the\n" + "Sniper. This game's move order is therefore defined as 'The Deferred Sniper'\n" + "simply for reference purposes and for future classification of Sniper games.\n" + "--- As I have resurrected this 4...c5 gambit in modern times and brought some\n" + "new enhancements, and as a struggling FM who would like to leave my stamp on\n" + "the chess kingdom, I have called it the 'Storey Gambit', which is simply\n" + "useful for reference purposes. Many years ago Grandmasters Gulko and Razuvaev\n" + "played 4...c5, but it has fallen into disuse and my recent contributions\n" + "should make it very appealing. --- In short, Black sacrifices the c5-pawn and\n" + "as a consequence prevents White from using that square for his knight. Keep an\n" + "eye on that square as you play through the game. Black progressively improves\n" + "his pieces better than White, and as a result he is the one who obtains\n" + "central control. White may hang on to the pawn on c5 for some time, but just\n" + "as in the Benko, Black's superior development ensures that the weak pawn will\n" + "eventually fall, after which Black's pieces can take further advantage of the\n" + "better squares they occupy to win another pawn or to initiate an attack on\n" + "White's king.} 5. dxc5 Nf6 $1 {Black is in no immediate hurry to capture the\n" + "weak pawn on c5. He can still keep a very good position by continuing with\n" + "development, gaining advantages in the centre and then capturing on c5 at an\n" + "opportune moment, possibly as late as the endgame.} 6. exd5 Qxd5 7. Nb3 Qxd1+\n" + "8. Kxd1 O-O 9. Nf3 Rd8+ 10. Ke1 Nc6 11. Be2 e5 12. Be3 Be6 13. Ng5 Bd5 14. f3\n" + "h6 15. Nh3 Be6 16. Rd1 Nd5 17. Bd2 f5 18. Nf2 a5 19. Nc1 a4 20. a3 Nf6 21. Ncd3\n" + "Bb3 22. Ra1 {. Having outplayed a 2600-Grandmaster up until here, I became too\n" + "excited. That is the only explanation I can give for playing the poor move} Ra5\n" + "$2 {. Unfortunately, this allowed Tiviakov to find good counterplay, and he\n" + "went on to get the better of the position in my time pressure.} ({Instead} \n" + "22... Rd7 $1 {is the simple and logical move. Let us see how the game could\n" + "have proceeded had I found the correct continuation (rather than 22...Ra5):}\n" + "23. g4 Rad8 24. gxf5 gxf5 25. Bc1 {White's position is inferior as his pieces\n" + "are passive, his king position is poor, and his pawn structure is weak. This\n" + "can be contrasted with Black's well placed minor pieces, superb rooks and\n" + "central control that all together spell danger for White.} Kf7 26. Rg1 Bf8 27.\n" + "Kf1 Bc4 28. Nb4 Bxe2+ 29. Kxe2 Na5 30. Nbd3 Re8 31. Rd1 Nc4 {Black's advantage\n" + "is clear: he has control of the centre, better squares piece for piece, and\n" + "the more active - yet safer - king. All that adds up to a significant plus,\n" + "even though White has an extra pawn. I hope this position convinces you there\n" + "is something worthwhile to the Sniper. The important thing for the Sniper\n" + "practitioner is that the opening was a tremendous success. If you get the\n" + "opportunity to play this variation - do so.}) *\n\n" + "{This game has particular emotional interest for me, because Yang Fan Zhou\n" + "played top board for the English under 16 team at the 2009 Olympiad, and for\n" + "the first time I was appointed coach to the England squad, and accompanied\n" + "them to the event as coach and manager. I had won over Yang Fan to the idea of\n" + "using the Sniper, albeit via the Sicilian Sniper move order, and he gave a\n" + "near perfect performance, gaining a superb victory against a young Armenian\n" + "Grandmaster who was the top-rated player participating in the event.} 1. e4 c5\n" + "({You may straight away be puzzled as to why 1...c5 was chosen when the moves .\n" + "..g6, ...Bg7 and ...c5 are all pre-determined by the system. Well, the point\n" + "is this: when you become experienced with the Sniper you will find that you\n" + "can choose the Sicilian Sniper move order 1...c5, even though the main essence\n" + "of the Sniper is to play the Pure Sniper sequence} 1... g6 {, 2...Bg7, 3...c5!\n" + "In fact, I strongly recommend you stay with the Pure Sniper move order until\n" + "you have mastered all of the material in this book. Only then should you\n" + "consider 1...c5 as a Sniper move, perhaps to avoid your opponent's preparation.\n" + "--- To further help the reader appreciate some of the move orders that make up\n" + "the Sniper, Yang Fan heads for an Extended Pure Sniper Transposition at 7...\n" + "Nc6. An Extended Pure Sniper occurs when a position that could have come from\n" + "a Pure Sniper is reached not at move three, but deeper into the opening.\n" + "Extended Pure Snipers can be a bit difficult deciphering, and this is the main\n" + "reason I recommend the reader to always view the variations from the 1...g6, 2.\n" + "..Bg7, 3...c5! perspective, at least until they have read this entire book.})\n" + "2. Nf3 d6 3. d4 cxd4 4. Nxd4 Nf6 5. Nc3 g6 6. Be3 Bg7 {We now have a mainline\n" + "Sicilian Dragon which could have also come via a Pure Sniper move order as\n" + "follows: 1 e4 g6 2 d4 Bg7 3 Nc3 c5 4 Nf3 cxd4 5 Nxd4 d6 6 Be3 Nf6. Of course\n" + "this only transposes if both parties are willing, and it is the nature of a\n" + "Sniper practitioner to search for interesting uncharted positions that can be\n" + "found by playing the Pure Sniper move order, and to aim for those positions.\n" + "For example, Black could deviate at move four with 4...Qa5, or instead of 6...\n" + "Nf6 with 6...h5!?. There are countless creative possibilities, and even though\n" + "this book will equip you with many, this could be fertile ground for your own\n" + "research too. --- What is the difference between a Pure Sniper and a Sicilian\n" + "Sniper? Why not just play the Sicilian move order? -- 1. In a Pure Sniper\n" + "Black can often avoid exchanging on d4 and instead develop other pieces first,\n" + "as will be shown later in this chapter. It will also be shown that this can be\n" + "highly beneficial for Black. -- 2. Black may not wish to play ...d6 at all but\n" + "in fact play ...d5 in one turn, taking play along similar lines to a\n" + "favourable Accelerated Dragon. -- 3. Black will attempt to play the 'Sniper\n" + "Sacrifice' (a trendy way of describing the Sniper bishop on g7 exchanging\n" + "itself for the c3-knight with ...Bxc3, which will often involve the temporary\n" + "or permanent sacrifice of the c5-pawn) at every possible favourable moment,\n" + "with the aim of achieving an excellent or dynamically equal position, so the\n" + "knight on g8 is left at home for as long as possible. --- This is the beauty\n" + "of the Sniper bishop. It has a selfless desire to sacrifice itself to secure\n" + "the centre for the rest of the troops, who can then quickly use it launch an\n" + "attack on the newly weakened white pawn structure on a2, c2 and c3. ---\n" + "Another great bonus of learning the Sniper is that all the ideas can be\n" + "tweaked simply by playing the Sniper with White as well! This can be achieved\n" + "simply by starting with a very useful waiting move of 1 a3!, and then playing\n" + "just like a Black Sniper but with the bonus of having control of the b4-square.\n" + "I have tried this myself to date in two rated games, with an ECF rating\n" + "performance of 240 (2520 FIDE) - that is with no specific study but rather\n" + "simply applying the principles of Black's Sniper play over to White and being\n" + "aware of the differences created by the bonus move 1 a3.} 7. f3 Nc6 {The\n" + "pressure created by the c6-knight on the d4-square is considered by theory to\n" + "be very useful. I strongly recommend ...Nc6 as part of the Sniper repertoire\n" + "when Black plays a Dragon. This is in conjunction with ...h5 or - if you're\n" + "feeling creative - ...h6, but both with delayed castling. You will find this a\n" + "very effective antidote to playing those who have sharpened their h2-pawn\n" + "battering ram, normally a simple and effective way to easy rook development\n" + "and a free attack against Dragon players. --- This ...Nc6/...h5/delayed ...0-0\n" + "Sniper Dragon will also ensure that your prized g7 Sniper soldier will be\n" + "firmly protected from its opposite number - White's dark-squared bishop. This\n" + "approach of delaying ...0-0 makes it a far more attractive proposition for\n" + "anyone looking to play the Sicilian Dragon.} 8. Bc4 {The young Armenian\n" + "Grandmaster tries a sideline in an attempt to confuse Yang Fan.} (8. Qd2 O-O {\n" + "is covered in the Magnus Carlsen section, but if you're looking for a novel\n" + "alternative I can strongly recommend the following Black piece deployment: ...\n" + "Bd7, ...Rc8, ...h6 or ...h5 and delaying ...0-0.}) 8... Bd7 {There's no need\n" + "to castle, as the tempo is far better served by accelerating an attack on to\n" + "the c4-bishop which has no communication with its allies. --- This system of\n" + "development (...Bd7, ...Rc8, ...h6 or ...h5, and delayed ...0-0) is not so\n" + "dependent on concrete variations but mainly ideas based. If it becomes more\n" + "mainline in the future then concrete variations will surface, but for the\n" + "foreseeable future there is much fertile ground for simply playing chess\n" + "within a safe framework of development. --- According to my own study of\n" + "players below 2700 there is an overwhelming tendency to castle when a piece\n" + "pressures a weak square around the king, but Sniper trainees and 2700+ players\n" + "know these squares are easily defended and therefore they only castle when\n" + "they have to, or if the rook is needed for rapid deployment.} (8... Qb6 $1 {\n" + "is a highly underrated move and one I recommend with confidence. This is\n" + "covered in the next game.}) 9. Qd2 Rc8 ({More normal is} 9... O-O {here;\n" + "however, the England camp had decided not to play by pure theory but always\n" + "look to delay ...0-0 for as long as safely possible, thus allowing one extra\n" + "move for central concerns. This, coupled with the ...Na5 attack on the\n" + "light-squared bishop, proves extremely problematic for the Armenian\n" + "Grandmaster.}) ({The alternative} 9... Rb8 {, using a delayed ...0-0 scheme of\n" + "development, may seem attractive. Let's call it the 'delayed ...0-0 Chinese\n" + "Dragon'. Well, I do not recommend the delayed ...0-0 Chinese Dragon in this\n" + "situation, as the following short variation will prove. I have included it to\n" + "show an instance of when delaying ...0-0, albeit a rarity, does not work\n" + "effectively:} 10. O-O-O h5 {neglects the centre and White is allowed a\n" + "breakthrough after} 11. Nxc6 bxc6 12. e5 dxe5 13. Ne4 Nd5 14. Bxd5 cxd5 15.\n" + "Qxd5 {, when the d-file pressure gives White a small advantage.}) 10. Bb3 Na5\n" + "$5 {I really like this...Na5 idea, which is a popular way of playing against\n" + "the White Sicilian set-up. When White cannot crack open the h-file, the best\n" + "plan for him is to centralize rooks and then play f4 followed by e5 with a\n" + "great central advantage. This ...Na5 'knight on the rim' idea prevents the\n" + "central initiative that White's f4 may bring and also it allows ...Nxb3 at a\n" + "moment of Blacks choosing.} 11. O-O-O a6 {Black has still not committed his\n" + "king to the kingside and thus is still preventing a cheap 'all-out attack' by\n" + "White. On the other hand, Black has clear coordinates on White's king location.\n" + "} 12. Kb1 b5 13. g4 {This attempts to discourage ...h5 should White play h2-h4,\n" + "which is good from the viewpoint of preventing a solid defensive set-up by\n" + "Black on the kingside, but it does allow Black an extra tempo on the queenside.\n" + "Yang Fan uses this to set his attack in motion and gain a great share in the\n" + "centre, which turns into more tangible advantages as the game develops.} Nc4 ({\n" + "Yang Fan prefers the traditional approach and rightly rejects} 13... Nxb3 {\n" + "as this makes it difficult to attack the white king. For example,} 14. Nxb3 O-O\n" + "({after} 14... Be6 15. Nd4 O-O 16. Nxe6 fxe6 17. Ne2 Qe8 18. Nd4 Qf7 19. h4 e5\n" + "{Black is passive but solid}) 15. h4 a5 16. h5 a4 17. Nd4 b4 18. Nd5 Nxd5 19.\n" + "exd5 b3 20. axb3 axb3 21. Nxb3 Qc7 {when White is more comfortable and can\n" + "expect to go on to win.}) 14. Bxc4 Rxc4 15. h4 b4 (15... h5 {may be better, as\n" + "it stops 16 h5 which could have caused some awkward defensive problems for\n" + "Black. Furthermore, 15...h5 would halt White's 'straightforward attack' in its\n" + "tracks and force him to search for another solution. For example,} 16. g5 Nh7\n" + "17. Nd5 Nf8 {(an added bonus of delaying ...0-0 is that this move is available)\n" + "} 18. Rhe1 Ne6 19. Nb3 {.}) ({Clearly 15...b4 is stronger than} 15... O-O $6 {\n" + ". Black has no urgent need to castle unless the rook is required for duties,\n" + "and it is exactly moves like ...b4 that are the reason why Sniper players\n" + "rarely castle kingside at an early stage. If Black had castled at an early\n" + "stage then he would likely be mauled on the kingside with a speculative\n" + "sacrifice that no Dragon/Sniper player would want to face.}) 16. Nce2 a5 {\n" + "Still not castling yet. The tempo saved could be critical as it is allowing\n" + "and disallowing a number of short and long-term plans, and this is mainly\n" + "beneficial to Black.} 17. Ng3 $6 {A major strategic error;} ({it was better to\n" + "play} 17. h5 $1 {with good attacking prospects. --- Yang Fan's next move is\n" + "highly instructive - it prevents further h-file danger, encourages closure of\n" + "the g- and h-files and also helps to prevent attacks in the centre by moving\n" + "the knight to the h7-square releasing the g7-bishop's energy.}) 17... h5 $1 {\n" + "17 moves in and Black has pressured the centre and expanded on the queenside.\n" + "The pressure of the extra attacking move gained by not castling has actually\n" + "had an exponential effect on Black's queenside counterplay and could be held\n" + "responsible for 'wasting' the knight's time moving from c3-e2-g3, thus\n" + "engineering a major strategical error in White's plan which in turn brings\n" + "about Black's eventual win.} 18. g5 Nh7 19. f4 {If White can play e5 Black may\n" + "be in big trouble especially with his king in the centre!} Bg4 $1 {Sending in\n" + "'disruptor' - this starts action against White's centre by removing the white\n" + "rook's influence over the central file. It is highly instructive how Black\n" + "fights for the centre and increases advantages there before delivering a final\n" + "assault on the king.} 20. Rc1 O-O {Just what did Black achieve by delaying ...\n" + "0-0? Well, he has: -- 1. The two bishops; -- 2. Advanced queenside pressure;\n" + "-- 3. A very safe king (no files open); -- and 4. Forced a non-standard plan\n" + "upon White, as well as persuading the knight to go to the poor square at g3.}\n" + "21. f5 Be5 {More central control and more indirect pressure on the centre.\n" + "Rybka still thinks this is level, but I claim Black has an almost decisive\n" + "advantage. I would argue that, with the better centre, more realistic\n" + "attacking chances on the white king, options to trade minor pieces, and with\n" + "White's weak e4-pawn, this is highly favourable for Black.} 22. Nge2 $6 {\n" + "Probably the fatal error by the Armenian Grandmaster - White is now doomed to\n" + "a long-term defence that is probably just hoping for an error to survive. Yang\n" + "Fan shows a level of technique way above his years and rating to bring home\n" + "the point for England - each move is quite instructive.} Qa8 {A superb way to\n" + "pressure the weak central pawn and allow the f8-rook to participate.} 23. b3\n" + "Rcc8 24. Qd3 Rfd8 {How many players would rush to attack an enemy king? Yang\n" + "Fan prefers to mobilize his redundant pieces (rook and knight) thus keeping\n" + "good control of the centre before the clinical finish. The knight on h7 still\n" + "has to make an important contribution to the attack and has an easy way in via\n" + "d7.} 25. c4 a4 {This 'tin opener' move attempts to open the a-file and weaken\n" + "the white king position.} 26. bxa4 Qxa4 {The white king begins to feel the\n" + "draught.} 27. Qb3 Qa8 {The queen shows that she can demonstrate her power on\n" + "the centre even from the corners.} 28. Qd3 Rb8 {'X-Ray check' is the first\n" + "major warning sign for White.} 29. Rhe1 Nf8 {The cavalry is set to arrive and\n" + "the Rybka chess engine is very happy with Black (-0.98). In English, that\n" + "means clear advantage for Black.} 30. Nb3 Bf3 {Yang Fan's pressure on the\n" + "centre in this game has been grandmasterly.} 31. Nd2 b3 $1 {The clinical\n" + "finish commences - now is the time for Yang Fan's calculation ability to be\n" + "demonstrated.} 32. axb3 Rxb3+ $1 {Dramatic measures to increase the weakness\n" + "of the white king.} 33. Qxb3 Rb8 {There is a defence to the pin but Yang Fan\n" + "has a great way to develop his final piece into the attack.} 34. Bb6 Bxe4+ 35.\n" + "Nxe4 Qxe4+ 36. Rc2 Nd7 $1 {The knight finds its way into the game.} 37. Kc1\n" + "Rxb6 {Black has a material deficit but enjoys the following advantages: -- 1.\n" + "The centre; -- 2. The much safer king; -- 3. The initiative; -- 4. More simple\n" + "targets to attack; -- and 5. No obvious targets of his own for White to attack.\n" + "All this blended together makes for a decisive advantage.} 38. Qa3 Nc5 39. Kd1\n" + "Nd3 40. Rf1 Rb1+ {White's position is a shambles. Yang Fan holds his nerve to\n" + "deliver a simple technical finish.} 41. Nc1 Nxc1 42. Rxc1 Rxc1+ 43. Qxc1 Qd3+\n" + "44. Ke1 Bg3+ 45. Rf2 {. The Armenian Grandmaster resigns and history is made,\n" + "as an English under-16 defeats a Grandmaster at this event for the first time.\n" + "--- This game saw a Sicilian mainline Dragon via the Sicilian Sniper move\n" + "order, but it equally could have arisen via a Pure Sniper move order. The\n" + "Sicilian Dragon is one of the variations that the Sniper player may allow. ---\n" + "Black played a delayed ...0-0 with ...Na5, and this way of playing the Dragon\n" + "clearly has some excellent benefits. Using the move saved by delaying ...0-0\n" + "enables Black to defend by central means or gain some queenside initiative. In\n" + "other words, bringing in extra support to the centre or removing a piece that\n" + "is pressuring the centre can help prevent an attack coming from its source.\n" + "This is a subtle form of defensive prophylaxis which fights against ...0-0\n" + "apathy that is prevalent from beginner and even up to Grandmaster level.\n" + "Sniper players only castle when their king is genuinely about to come under\n" + "fire, or when the rook is urgently needed for central matters. These Sniper\n" + "principles defeated one of the best juniors in the world.} 0-1"; final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE | Pattern.COMMENTS); final Matcher matcher = pattern.matcher(string); while (matcher.find()) { System.out.println("Full match: " + matcher.group(0)); for (int i = 1; i <= matcher.groupCount(); i++) { System.out.println("Group " + i + ": " + matcher.group(i)); } } } }

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 Java, please visit: https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html