<p>So I'm working on a project that will allow users to enter <strong>poker hand histories</strong> from sites like PokerStars and then display the hand to them.</p>

<p>It seems that regex would be a great tool for this, however I rank my regex knowledge at "slim to none".</p>

<p>So I'm using PHP and looping through this block of text line by line and on lines like this:</p>

<pre><code>Seat 1: fabulous29 (835 in chips)

Seat 2: Nioreh_21 (6465 in chips)

Seat 3: Big Loads (3465 in chips)

Seat 4: Sauchie (2060 in chips)
</code></pre>

<p>I want to extract seat <code>number, name, &amp; chip count</code> so the format is</p>

<pre><code>Seat [number]: [letters&amp;numbers&amp;characters] ([number] in chips)
</code></pre>

<p>I have NO IDEA where to start or what commands I should even be using to optimize this.</p>

<p>Any advice is greatly appreciated - even if it is just a link to a tutorial on PHP regex or the name of the command(s) I should be using.</p>

[{'answer_id': 16569, 'author': 'Ellen Teapot', 'author_id': 1914, 'author_profile': 'https://Stackoverflow.com/users/1914', 'pm_score': 2, 'selected': False, 'text': '<p>Look at the PCRE section in the PHP Manual. Also, <a href="http://www.regular-expressions.info/" rel="nofollow noreferrer">http://www.regular-expressions.info/</a> is a great site for learning regex. Disclaimer: Regex is very addictive once you learn it.</p>\n'}, {'answer_id': 16575, 'author': 'Adam Haile', 'author_id': 194, 'author_profile': 'https://Stackoverflow.com/users/194', 'pm_score': 2, 'selected': False, 'text': '<p>I\'m not entirely sure what exactly to use for that without trying it, but a great tool I use all the time to validate my RegEx is <a href="http://gskinner.com/RegExr/" rel="nofollow noreferrer">RegExr</a> which gives a great flash interface for trying out your regex, including real time matching and a library of predefined snippets to use.  Definitely a great time saver :)</p>\n'}, {'answer_id': 16583, 'author': 'Brian Warshaw', 'author_id': 1344, 'author_profile': 'https://Stackoverflow.com/users/1344', 'pm_score': 2, 'selected': False, 'text': "<p>I always use the preg_ set of function for REGEX in PHP because the PERL-compatible expressions have much more capability. That extra capability doesn't necessarily come into play here, but they are also supposed to be faster, so why not use them anyway, right?</p>\n\n<p>For an expression, try this:</p>\n\n<pre><code>/Seat (\\d+): ([^ ]+) \\((\\d+)/\n</code></pre>\n\n<p>You can use preg_match() on each line, storing the results in an array. You can then get at those results and manipulate them as you like.</p>\n\n<p>EDIT:</p>\n\n<p>Btw, you could also run preg<code>_</code>match<code>_</code>all on the entire block of text (instead of looping through line-by-line) and get the results that way, too.</p>\n"}, {'answer_id': 16592, 'author': 'Kibbee', 'author_id': 1862, 'author_profile': 'https://Stackoverflow.com/users/1862', 'pm_score': -1, 'selected': False, 'text': '<pre><code>Seat [number]: [letters&amp;numbers&amp;characters] ([number] in chips)\n</code></pre>\n\n<p>Your Regex should look something like this</p>\n\n<pre><code>Seat (\\d+): ([a-zA-Z0-9]+) \\((\\d+) in chips\\)\n</code></pre>\n\n<p>The brackets will let you capture the seat number, name and number of chips in groups.</p>\n'}, {'answer_id': 16593, 'author': 'Roy Rico', 'author_id': 1580, 'author_profile': 'https://Stackoverflow.com/users/1580', 'pm_score': -1, 'selected': False, 'text': '<p>you\'ll have to split the file by linebreaks,\nthen loop thru each line and apply the following logic</p>\n\n<pre><code>$seat = 0;\n$name = 1;\n$chips = 2;\n\nforeach( $string in $file ) {\n  if (preg_match("Seat ([1-0]): ([A-Za-z_0-9]*) \\(([1-0]*) in chips\\)", $string, $matches)) {\n    echo "Seat: " . $matches[$seat] . "&lt;br&gt;";\n    echo "Name: " . $matches[$name] . "&lt;br&gt;";\n    echo "Chips: " . $matches[$chips] . "&lt;br&gt;";\n  }\n}\n</code></pre>\n\n<p>I haven\'t ran this code, so you may have to fix some errors...</p>\n'}, {'answer_id': 16598, 'author': 'Andrew G. Johnson', 'author_id': 428190, 'author_profile': 'https://Stackoverflow.com/users/428190', 'pm_score': 0, 'selected': False, 'text': '<p>Here\'s what I\'m currently using:</p>\n\n<pre><code>preg_match("/(Seat \\d+: [A-Za-z0-9 _-]+) \\((\\d+) in chips\\)/",$line)\n</code></pre>\n'}, {'answer_id': 16603, 'author': 'Joel Meador', 'author_id': 1976, 'author_profile': 'https://Stackoverflow.com/users/1976', 'pm_score': 1, 'selected': False, 'text': '<p>Check out <a href="http://www.php.net/manual/en/function.preg-match.php" rel="nofollow noreferrer">preg_match</a>.\nProbably looking for something like...</p>\n\n<pre><code>&lt;?php\n$str = \'Seat 1: fabulous29 (835 in chips)\';\npreg_match(\'/Seat (?&lt;seatNo&gt;\\d+): (?&lt;name&gt;\\w+) \\((?&lt;chipCnt&gt;\\d+) in chips\\)/\', $str, $matches);\nprint_r($matches);\n?&gt;\n</code></pre>\n\n<p>*It\'s been a while since I did php, so this <em>could</em> be a little or a lot off.*</p>\n'}, {'answer_id': 16643, 'author': 'Andy', 'author_id': 1993, 'author_profile': 'https://Stackoverflow.com/users/1993', 'pm_score': 2, 'selected': False, 'text': '<p>Something like this might do the trick:</p>\n\n<pre><code>/Seat (\\d+): ([^\\(]+) \\((\\d+)in chips\\)/\n</code></pre>\n\n<p>And some basic explanation on how Regex works:</p>\n\n<ul>\n<li><p>\\d = digit.</p></li>\n<li><p>\\&lt;character> = escapes character, if not part of any character class or subexpression. for example: </p>\n\n<p><code>\\t</code> \nwould render a tab, while <code>\\\\t</code> would render "\\t" (since the backslash is escaped).</p></li>\n<li><p>+ = one or more of the preceding element.</p></li>\n<li><p>* = zero or more of the preceding element.</p></li>\n<li><p>[ ] =  bracket expression. Matches any of the characters within the bracket. Also works with ranges (ex. A-Z).</p></li>\n<li><p>[^ ] = Matches any character that is NOT within the bracket.</p></li>\n<li><p>( ) = Marked subexpression. The data matched within this can be recalled later.</p></li>\n</ul>\n\n<p>Anyway, I chose to use </p>\n\n<pre><code>([^\\(]+)\n</code></pre>\n\n<p>since the example provides a name containing spaces (Seat 3 in the example). what this does is that it matches any character up to the point that it encounters an opening paranthesis.\nThis will leave you with a blank space at the end of the subexpression (using the data provided in the example). However, his can easily be stripped away using the trim() command in PHP.</p>\n\n<p>If you do not want to match spaces, only alphanumerical characters, you could so something like this:</p>\n\n<pre><code>([A-Za-z0-9-_]+)\n</code></pre>\n\n<p>Which would match any letter (within A-Z, both upper- &amp; lower-case), number as well as hyphens and underscores.</p>\n\n<p>Or the same variant, with spaces:</p>\n\n<pre><code>([A-Za-z0-9-_\\s]+)\n</code></pre>\n\n<p>Where "\\s" is evaluated into a space.</p>\n\n<p>Hope this helps :)</p>\n'}, {'answer_id': 17522, 'author': 'Imran', 'author_id': 1897, 'author_profile': 'https://Stackoverflow.com/users/1897', 'pm_score': 0, 'selected': False, 'text': "<p>To process the whole input string at once, use <code>preg_match_all()</code></p>\n\n<pre><code>preg_match_all('/Seat (\\d+): \\w+ \\((\\d+) in chips\\)/', $preg_match_all, $matches);\n</code></pre>\n\n<p>For your input string, var_dump of $matches will look like this:</p>\n\n<pre><code>array\n  0 =&gt; \n    array\n      0 =&gt; string 'Seat 1: fabulous29 (835 in chips)' (length=33)\n      1 =&gt; string 'Seat 2: Nioreh_21 (6465 in chips)' (length=33)\n      2 =&gt; string 'Seat 4: Sauchie (2060 in chips)' (length=31)\n  1 =&gt; \n    array\n      0 =&gt; string '1' (length=1)\n      1 =&gt; string '2' (length=1)\n      2 =&gt; string '4' (length=1)\n  2 =&gt; \n    array\n      0 =&gt; string '835' (length=3)\n      1 =&gt; string '6465' (length=4)\n      2 =&gt; string '2060' (length=4)\n</code></pre>\n\n<p><strong>On learning regex</strong>: Get Mastering Regular Expressions, 3rd Edition. Nothing else comes close to the this book if you really want to learn regex. Despite being the definitive guide to regex, the book is very beginner friendly.</p>\n"}, {'answer_id': 19360596, 'author': 'A. Zalonis', 'author_id': 2455661, 'author_profile': 'https://Stackoverflow.com/users/2455661', 'pm_score': 0, 'selected': False, 'text': '<p>Try this code. It works for me</p>\n\n<p>Let say that you have below lines of strings</p>\n\n<pre><code>$string1 = "Seat 1: fabulous29 (835 in chips)";\n$string2 = "Seat 2: Nioreh_21 (6465 in chips)";\n$string3 = "Seat 3: Big Loads (3465 in chips)";\n$string4 = "Seat 4: Sauchie (2060 in chips)";\n</code></pre>\n\n<p>Add to array</p>\n\n<pre><code>$lines = array($string1,$string2,$string3,$string4);\nforeach($lines as $line )\n{\n  $seatArray = explode(":", $line);\n  $seat = explode(" ",$seatArray[0]);\n  $seatNumber = $seat[1];\n\n  $usernameArray = explode("(",$seatArray[1]);\n  $username = trim($usernameArray[0]);\n\n  $chipArray = explode(" ",$usernameArray[1]);\n  $chipNumber = $chipArray[0]; \n\n  echo "&lt;br&gt;"."Seat [".$seatNumber."]: [". $username."] ([".$chipNumber."] in chips)";\n}\n</code></pre>\n'}, {'answer_id': 26624125, 'author': 'Suganthan Madhavan Pillai', 'author_id': 2534236, 'author_profile': 'https://Stackoverflow.com/users/2534236', 'pm_score': 1, 'selected': False, 'text': '<p>May be it is very late answer, But I am interested in answering</p>\n\n<pre><code>Seat\\s(\\d):\\s([\\w\\s]+)\\s\\((\\d+).*\\)\n</code></pre>\n\n<p><a href="http://regex101.com/r/cU7yD7/1" rel="nofollow">http://regex101.com/r/cU7yD7/1</a></p>\n'}]
['https://Stackoverflow.com/questions/16563', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/428190/']