04-21-2020, 09:28 PM
The Dot Character in a Character Set – What Does It Match?
<div><p>Given is the following regular expression:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">regex = '[a-z.]+'</pre>
<p>Note the <a href="https://blog.finxter.com/python-re-dot/" target="_blank" rel="noreferrer noopener">dot character</a> inside the character set. As you may know, the dot metacharacter matches an arbitrary character if it is used outside a <a href="https://blog.finxter.com/python-character-set-regex-tutorial/" target="_blank" rel="noreferrer noopener">character set</a>. </p>
<p><strong>But what does it match if you place the dot character <a href="https://blog.finxter.com/does-the-dot-regex-match-whitespace-characters-in-python/" target="_blank" rel="noreferrer noopener">inside a regex character set</a>?</strong></p>
<p><strong>The answer is that the dot inside the character set matches the dot symbol—and not an arbitrary character. The reason is that the character set removes the special meaning of the dot symbol. </strong></p>
<p>Here’s a simple example:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import re regex = '[a-z.]+' text_1 = 'hello.world'
text_2 = 'HELLO.WORLD' print(re.match(regex, text_1))
# <re.Match object; span=(0, 11), match='hello.world'> print(re.match(regex, text_2))
# None</pre>
<p>The first text will be matched in both cases (the dot character matches an arbitrary character or the dot symbol). </p>
<p>But the second text will only match if the dot has the meaning: <em>“match an arbitrary character”</em>. Otherwise, the character set cannot match the text. </p>
<p>As the result is None, the text could not have been matched. <strong>This proves that the dot metacharacter loses its special meaning inside a character set.</strong></p>
</div>
https://www.sickgaming.net/blog/2020/04/...-it-match/
<div><p>Given is the following regular expression:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">regex = '[a-z.]+'</pre>
<p>Note the <a href="https://blog.finxter.com/python-re-dot/" target="_blank" rel="noreferrer noopener">dot character</a> inside the character set. As you may know, the dot metacharacter matches an arbitrary character if it is used outside a <a href="https://blog.finxter.com/python-character-set-regex-tutorial/" target="_blank" rel="noreferrer noopener">character set</a>. </p>
<p><strong>But what does it match if you place the dot character <a href="https://blog.finxter.com/does-the-dot-regex-match-whitespace-characters-in-python/" target="_blank" rel="noreferrer noopener">inside a regex character set</a>?</strong></p>
<p><strong>The answer is that the dot inside the character set matches the dot symbol—and not an arbitrary character. The reason is that the character set removes the special meaning of the dot symbol. </strong></p>
<p>Here’s a simple example:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import re regex = '[a-z.]+' text_1 = 'hello.world'
text_2 = 'HELLO.WORLD' print(re.match(regex, text_1))
# <re.Match object; span=(0, 11), match='hello.world'> print(re.match(regex, text_2))
# None</pre>
<p>The first text will be matched in both cases (the dot character matches an arbitrary character or the dot symbol). </p>
<p>But the second text will only match if the dot has the meaning: <em>“match an arbitrary character”</em>. Otherwise, the character set cannot match the text. </p>
<p>As the result is None, the text could not have been matched. <strong>This proves that the dot metacharacter loses its special meaning inside a character set.</strong></p>
</div>
https://www.sickgaming.net/blog/2020/04/...-it-match/