Author: h.mohri
The 35th Developer camp will be held in Japan on March 15, 2018.
The venue is Tokyo Akihabara UDX GALLERY NEXT.
C++Builder 10.2.2 is one of the merits of being able to use RTL and STL.
For RTL, the classes on Regular expressions are TRegEx, TPerlRegEx.
But, since std::regex
STL can also be used, I set the target device to iOS this time and tried it.
I tried four patterns. The pattern numbers, hiragana-Japanese, ASCII and e-mail.
C++Builder 10.2.2 code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
<span class="cp" style="color: #999999; font-weight: bold; font-style: italic;">#include</span> <span class="cpf"><string></span> <span class="cp" style="color: #999999; font-weight: bold; font-style: italic;">#include</span> <span class="cpf"><regex></span> <span class="cp" style="color: #999999; font-weight: bold; font-style: italic;">#include</span> <span class="cpf"><functional></span> <span class="cp" style="color: #999999; font-weight: bold; font-style: italic;">#include</span> <span class="cpf"><vector></span> <span class="cp" style="color: #999999; font-weight: bold; font-style: italic;">#include</span> <span class="cpf"><tuple></span> <span class="c1" style="color: #999988; font-style: italic;">//---------------------------------------------------------------------------</span> <span class="kt" style="color: #445588; font-weight: bold;">void</span> <span class="kr" style="font-weight: bold;">__fastcall</span> <span class="n">TForm1</span><span class="o" style="font-weight: bold;">::</span><span class="n">SpeedButton1Click</span><span class="p">(</span><span class="n">TObject</span> <span class="o" style="font-weight: bold;">*</span><span class="n">Sender</span><span class="p">)</span> <span class="p">{</span> <span class="n">std</span><span class="o" style="font-weight: bold;">::</span><span class="n">vector</span><span class="o" style="font-weight: bold;"><</span><span class="n">std</span><span class="o" style="font-weight: bold;">::</span><span class="n">tuple</span><span class="o" style="font-weight: bold;"><</span><span class="n">std</span><span class="o" style="font-weight: bold;">::</span><span class="n">wstring</span><span class="p">,</span> <span class="n">std</span><span class="o" style="font-weight: bold;">::</span><span class="n">wstring</span><span class="o" style="font-weight: bold;">></span> <span class="o" style="font-weight: bold;">></span> <span class="n">v1</span><span class="p">{</span> <span class="c1" style="color: #999988; font-style: italic;">//When you want to match numbers.</span> <span class="p">{</span><span class="sa">L</span><span class="s" style="color: #d01040;">"</span><span class="se" style="color: #d01040;">\</span><span class="s" style="color: #d01040;">d+"</span><span class="p">,</span> <span class="sa">L</span><span class="s" style="color: #d01040;">"regex_match(</span><span class="se" style="color: #d01040;">"\</span><span class="s" style="color: #d01040;">d+</span><span class="se" style="color: #d01040;">"</span><span class="s" style="color: #d01040;">,%s)"</span><span class="p">},</span> <span class="c1" style="color: #999988; font-style: italic;">//When you want to match Hiragana-Japanese.</span> <span class="p">{</span><span class="sa">L</span><span class="s" style="color: #d01040;">"^[</span><span class="se" style="box-sizing: inherit; color: #d01040;">u3040</span><span class="s" style="box-sizing: inherit; color: #d01040;">-</span><span class="se" style="box-sizing: inherit; color: #d01040;">u309F</span><span class="s" style="box-sizing: inherit; color: #d01040;">]+$"</span><span class="p">,</span> <span class="sa">L</span><span class="s" style="color: #d01040;">"regex_match(</span><span class="se" style="color: #d01040;">"</span><span class="s" style="color: #d01040;">^[</span><span class="se" style="box-sizing: inherit; color: #d01040;">u3040</span><span class="s" style="box-sizing: inherit; color: #d01040;">-</span><span class="se" style="box-sizing: inherit; color: #d01040;">u309F</span><span class="s" style="box-sizing: inherit; color: #d01040;">]+$</span><span class="se" style="color: #d01040;">"</span><span class="s" style="color: #d01040;">,%s)"</span><span class="p">},</span> <span class="c1" style="color: #999988; font-style: italic;">//When you want to match ASCII Code.</span> <span class="p">{</span><span class="sa">L</span><span class="s" style="color: #d01040;">"^[</span><span class="se" style="box-sizing: inherit; color: #d01040;">x20</span><span class="s" style="box-sizing: inherit; color: #d01040;">-</span><span class="se" style="box-sizing: inherit; color: #d01040;">x7E</span><span class="s" style="box-sizing: inherit; color: #d01040;">]+$"</span><span class="p">,</span> <span class="sa">L</span><span class="s" style="color: #d01040;">"regex_match(</span><span class="se" style="color: #d01040;">"</span><span class="s" style="color: #d01040;">^[</span><span class="se" style="box-sizing: inherit; color: #d01040;">x20</span><span class="s" style="box-sizing: inherit; color: #d01040;">-</span><span class="se" style="box-sizing: inherit; color: #d01040;">x7E</span><span class="s" style="box-sizing: inherit; color: #d01040;">]+$</span><span class="se" style="color: #d01040;">"</span><span class="s" style="color: #d01040;">,%s)"</span><span class="p">},</span> <span class="c1" style="color: #999988; font-style: italic;">//Determine whether the wstring is in E-mail format.</span> <span class="p">{</span><span class="sa">L</span><span class="s" style="color: #d01040;">"^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:.[a-zA-Z0-9-]+)*$"</span><span class="p">,</span> <span class="sa">L</span><span class="s" style="color: #d01040;">"regex_match(E-Mail,%s)"</span><span class="p">}</span> <span class="p">};</span> <span class="n">std</span><span class="o" style="font-weight: bold;">::</span><span class="n">function</span><span class="o" style="font-weight: bold;"><</span><span class="n">std</span><span class="o" style="font-weight: bold;">::</span><span class="n">wstring</span><span class="p">(</span><span class="n">UnicodeString</span><span class="p">)</span><span class="o" style="font-weight: bold;">></span> <span class="n">str_towstr</span><span class="p">{[](</span><span class="n">UnicodeString</span> <span class="n">linp</span><span class="p">)</span> <span class="p">{</span> <span class="n">std</span><span class="o" style="font-weight: bold;">::</span><span class="n">wstring</span> <span class="n">out</span><span class="p">;</span> <span class="k" style="font-weight: bold;">for</span> <span class="p">(</span><span class="kt" style="color: #445588; font-weight: bold;">int</span> <span class="n">i</span><span class="o" style="font-weight: bold;">=</span><span class="mi" style="color: #009999;">0</span><span class="p">;</span> <span class="n">i</span> <span class="o" style="font-weight: bold;"><</span> <span class="n">linp</span><span class="p">.</span><span class="n">Length</span><span class="p">();</span> <span class="n">i</span><span class="o" style="font-weight: bold;">++</span><span class="p">)</span> <span class="p">{</span> <span class="cp" style="color: #999999; font-weight: bold; font-style: italic;">#ifndef _WIN64</span> <span class="n">out</span> <span class="o" style="font-weight: bold;">+=</span><span class="n">linp</span><span class="p">[</span><span class="n" style="box-sizing: inherit;">i</span><span class="p" style="box-sizing: inherit;">];</span> <span class="cp" style="color: #999999; font-weight: bold; font-style: italic;">#else </span><span class="cm" style="color: #999988; font-style: italic;">/* _WIN64 */</span> <span class="n">out</span> <span class="o" style="font-weight: bold;">+=</span><span class="n">linp</span><span class="p">[</span><span class="n" style="box-sizing: inherit;">i</span><span class="o" style="box-sizing: inherit; font-weight: bold;">+</span><span class="mi" style="box-sizing: inherit; color: #009999;">1</span><span class="p" style="box-sizing: inherit;">];</span> <span class="cp" style="color: #999999; font-weight: bold; font-style: italic;">#endif</span> <span class="p">}</span> <span class="k" style="font-weight: bold;">return</span> <span class="n">out</span><span class="p">;</span> <span class="p">}</span> <span class="p">};</span> <span class="c1" style="color: #999988; font-style: italic;">//It was selected in the ComboBox1, the content you want to match.</span> <span class="n">std</span><span class="o" style="font-weight: bold;">::</span><span class="n">wregex</span> <span class="n">wregex1</span><span class="p">(</span><span class="n">std</span><span class="o" style="font-weight: bold;">::</span><span class="n">get</span><span class="o" style="font-weight: bold;"><</span><span class="mi" style="color: #009999;">0</span><span class="o" style="font-weight: bold;">></span><span class="p">(</span><span class="n">v1</span><span class="p">[</span><span class="n" style="box-sizing: inherit;">ComboBox1</span><span class="o" style="box-sizing: inherit; font-weight: bold;">-></span><span class="n" style="box-sizing: inherit;">ItemIndex</span><span class="p" style="box-sizing: inherit;">]).</span><span class="n">c_str</span><span class="p">());</span> <span class="c1" style="color: #999988; font-style: italic;">//The string you want to match, and assigned to the wstring.</span> <span class="n">std</span><span class="o" style="font-weight: bold;">::</span><span class="n">wstring</span> <span class="n">in_str</span> <span class="o" style="font-weight: bold;">=</span> <span class="n">str_towstr</span><span class="p">(</span><span class="n">Edit1</span><span class="o" style="font-weight: bold;">-></span><span class="n">Text</span><span class="p">);</span> <span class="n">UnicodeString</span> <span class="n">l_value</span> <span class="o" style="font-weight: bold;">=</span> <span class="n">Format</span><span class="p">(</span><span class="n">std</span><span class="o" style="font-weight: bold;">::</span><span class="n">get</span><span class="o" style="font-weight: bold;"><</span><span class="mi" style="color: #009999;">1</span><span class="o" style="font-weight: bold;">></span><span class="p">(</span><span class="n">v1</span><span class="p">[</span><span class="n" style="box-sizing: inherit;">ComboBox1</span><span class="o" style="box-sizing: inherit; font-weight: bold;">-></span><span class="n" style="box-sizing: inherit;">ItemIndex</span><span class="p" style="box-sizing: inherit;">]).</span><span class="n">c_str</span><span class="p">(),</span> <span class="n">ARRAYOFCONST</span><span class="p">((</span><span class="n">Edit1</span><span class="o" style="font-weight: bold;">-></span><span class="n">Text</span><span class="p">))</span> <span class="p">);</span> <span class="c1" style="color: #999988; font-style: italic;">//It is judged here. Use regex_match.</span> <span class="k" style="font-weight: bold;">if</span> <span class="p">(</span><span class="n">std</span><span class="o" style="font-weight: bold;">::</span><span class="n">regex_match</span><span class="p">(</span><span class="n">in_str</span><span class="p">,</span> <span class="n">wregex1</span><span class="p">))</span> <span class="p">{</span> <span class="c1" style="color: #999988; font-style: italic;">//If the regex_match was successful.</span> <span class="n">Memo1</span><span class="o" style="font-weight: bold;">-></span><span class="n">Lines</span><span class="o" style="font-weight: bold;">-></span><span class="n">Append</span><span class="p">(</span><span class="s" style="color: #d01040;">"Successful "</span> <span class="o" style="font-weight: bold;">+</span> <span class="n">l_value</span><span class="p">);</span> <span class="n">Edit1</span><span class="o" style="font-weight: bold;">-></span><span class="n">Text</span> <span class="o" style="font-weight: bold;">=</span> <span class="sa">L</span><span class="s" style="color: #d01040;">""</span><span class="p">;</span> <span class="p">}</span> <span class="k" style="font-weight: bold;">else</span> <span class="p">{</span> <span class="c1" style="color: #999988; font-style: italic;">//If the regex_match failed.</span> <span class="n">Memo1</span><span class="o" style="font-weight: bold;">-></span><span class="n">Lines</span><span class="o" style="font-weight: bold;">-></span><span class="n">Append</span><span class="p">(</span><span class="s" style="color: #d01040;">"NO "</span> <span class="o" style="font-weight: bold;">+</span> <span class="n">l_value</span><span class="p">);</span> <span class="p">}</span> <span class="n">Button1</span><span class="o" style="font-weight: bold;">-></span><span class="n">SetFocus</span><span class="p">();</span> <span class="p">}</span> |
Select a pattern in ComboBox1, enter a character string in Edit1, and tap SpeedButton1.
If a pattern match is found, Successful is displayed in Memo1.
Design. Code. Compile. Deploy.
Start Free Trial Upgrade Today
Free Delphi Community Edition Free C++Builder Community Edition