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

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

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 PHP, please visit: http://php.net/manual/en/ref.pcre.php