snipt

Ctrl+h for KB shortcuts

Ruby

Use '[' and ']' for regexps

1
2
3
4
5
6
7
8
# This will find the first numbers in an ip

ip = "123.33.1.200"
ip[/(\d+).(\d+).(\d+).(d+)/]

puts "First: #{$1}"
puts "Second: #{$2}"
# . . .
https://snipt.net/embed/96bfb625bdbe75fd3b4901cc6b79a77e/
https://snipt.net/raw/96bfb625bdbe75fd3b4901cc6b79a77e/
96bfb625bdbe75fd3b4901cc6b79a77e
rb
Ruby
8
2016-09-22T17:01:55
True
False
False
/api/public/snipt/11603/
use-and-for-regexps
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a> <a href="#L-3">3</a> <a href="#L-4">4</a> <a href="#L-5">5</a> <a href="#L-6">6</a> <a href="#L-7">7</a> <a href="#L-8">8</a></pre></div></td><td class="code"><div class="highlight"><pre><span id="L-1"><a name="L-1"></a><span class="c1"># This will find the first numbers in an ip</span> </span><span id="L-2"><a name="L-2"></a> </span><span id="L-3"><a name="L-3"></a><span class="n">ip</span> <span class="o">=</span> <span class="s2">&quot;123.33.1.200&quot;</span> </span><span id="L-4"><a name="L-4"></a><span class="n">ip</span><span class="o">[</span><span class="sr">/(\d+).(\d+).(\d+).(d+)/</span><span class="o">]</span> </span><span id="L-5"><a name="L-5"></a> </span><span id="L-6"><a name="L-6"></a><span class="nb">puts</span> <span class="s2">&quot;First: </span><span class="si">#{</span><span class="vg">$1</span><span class="si">}</span><span class="s2">&quot;</span> </span><span id="L-7"><a name="L-7"></a><span class="nb">puts</span> <span class="s2">&quot;Second: </span><span class="si">#{</span><span class="vg">$2</span><span class="si">}</span><span class="s2">&quot;</span> </span><span id="L-8"><a name="L-8"></a><span class="c1"># . . .</span> </span></pre></div> </td></tr></table>
ip, numbers, regexp, ruby

Java

Add leading 0

1
2
3
4
/*
 * Add 0 to stretch i to 2 digits.
 */
String.format("%02d", i);
https://snipt.net/embed/a636bb43a275ad4e4ffba5c9d2077e31/
https://snipt.net/raw/a636bb43a275ad4e4ffba5c9d2077e31/
a636bb43a275ad4e4ffba5c9d2077e31
java
Java
4
2016-09-21T17:45:49
True
False
False
/api/public/snipt/3654/
add-leading-zeroes
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a> <a href="#L-3">3</a> <a href="#L-4">4</a></pre></div></td><td class="code"><div class="highlight"><pre><span id="L-1"><a name="L-1"></a><span class="cm">/*</span> </span><span id="L-2"><a name="L-2"></a><span class="cm"> * Add 0 to stretch i to 2 digits.</span> </span><span id="L-3"><a name="L-3"></a><span class="cm"> */</span> </span><span id="L-4"><a name="L-4"></a><span class="n">String</span><span class="o">.</span><span class="na">format</span><span class="o">(</span><span class="s">&quot;%02d&quot;</span><span class="o">,</span> <span class="n">i</span><span class="o">);</span> </span></pre></div> </td></tr></table>
java, numbers, string
Copyrighted, illegal, or inappropriate content? Email support@snipt.net.