<p>How can I create a regular expression that will grab delimited text from a string?  For example, given a string like </p>

<pre><code>text ###token1### text text ###token2### text text 
</code></pre>

<p>I want a regex that will pull out <code>###token1###</code>.  Yes, I do want the delimiter as well.  By adding another group, I can get both:</p>

<pre><code>(###(.+?)###)
</code></pre>

[{'answer_id': 76427, 'author': 'David Beleznay', 'author_id': 13359, 'author_profile': 'https://Stackoverflow.com/users/13359', 'pm_score': 3, 'selected': True, 'text': '<pre><code>/###(.+?)###/\n</code></pre>\n\n<p>if you want the ###\'s  then you need</p>\n\n<pre><code>/(###.+?###)/\n</code></pre>\n\n<p>the <strong>?</strong> means non greedy, if you didn\'t have the <strong>?</strong>, then it would grab too much. </p>\n\n<p>e.g. <code>\'###token1### text text ###token2###\'</code> would all get grabbed.  </p>\n\n<p>My initial answer had a * instead of a +.  * means 0 or more.  + means 1 or more.  * was wrong because that would allow ###### as a valid thing to find. </p>\n\n<p>For playing around with regular expressions. I highly recommend <a href="http://www.weitz.de/regex-coach/" rel="nofollow noreferrer">http://www.weitz.de/regex-coach/</a> for windows.  You can type in the string you want and your regular expression and see what it\'s actually doing.  </p>\n\n<p>Your selected text will be stored in \\1   or $1 depending on where you are using your regular expression.</p>\n'}, {'answer_id': 76445, 'author': 'Jonathan Rupp', 'author_id': 12502, 'author_profile': 'https://Stackoverflow.com/users/12502', 'pm_score': 0, 'selected': False, 'text': '<p>Assuming you want to match ###token2### as well...</p>\n\n<pre><code>/###.+###/\n</code></pre>\n'}, {'answer_id': 76490, 'author': 'Colen', 'author_id': 13500, 'author_profile': 'https://Stackoverflow.com/users/13500', 'pm_score': 0, 'selected': False, 'text': "<p>Use () and \\x. A naive example that assumes the text within the tokens is always delimited by #:</p>\n\n<pre><code>text (#+.+#+) text text (#+.+#+) text text\n</code></pre>\n\n<p>The stuff in the () can then be grabbed by using \\1 and \\2 (\\1 for the first set, \\2 for the second in the replacement expression (assuming you're doing a search/replace in an editor). For example, the replacement expression could be:</p>\n\n<pre><code>token1: \\1, token2: \\2\n</code></pre>\n\n<p>For the above example, that should produce:</p>\n\n<pre><code>token1: ###token1###, token2: ###token2###\n</code></pre>\n\n<p>If you're using a regexp library in a program, you'd presumably call a function to get at the contents first and second token, which you've indicated with the ()s around them.</p>\n"}, {'answer_id': 76510, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 0, 'selected': False, 'text': '<p>Well when you are using delimiters such as this basically you just grab the first one then anything that does not match the ending delimiter followed by the ending delimiter. A special caution should be that in cases as the example above [^#] would not work as checking to ensure the end delimiter is not there since a singe # would cause the regex to fail (ie. "###foo#bar###). In the case above the regex to parse it would be the following assuming empty tokens are allowed (if not, change * to +):</p>\n\n<p>###([^#]|#[^#]|##[^#])*###</p>\n'}, {'answer_id': 76519, 'author': 'Michael Cramer', 'author_id': 1496728, 'author_profile': 'https://Stackoverflow.com/users/1496728', 'pm_score': 1, 'selected': False, 'text': '<p>In Perl, you actually want something like this:</p>\n\n<pre><code>$text = \'text ###token1### text text ###token2### text text\';\n\nwhile($text =~ m/###(.+?)###/g) {\n  print $1, "\\n";\n}\n</code></pre>\n\n<p>Which will give you each token in turn within the while loop. The (.*?) ensures that you get the <em>shortest</em> bit between the delimiters, preventing it from thinking the token is \'token1### text text ###token2\'.</p>\n\n<p>Or, if you just want to save them, not loop immediately:</p>\n\n<pre><code>@tokens = $text =~ m/###(.+?)###/g;\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/76411', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/410/']