Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] The Dot Character in a Character Set – What Does It Match?

#1
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))
# &lt;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/
Reply



Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tut] Python RegEx – Match Whitespace But Not Newline xSicKxBot 0 612 08-02-2022, 09:58 PM
Last Post: xSicKxBot
  [Tut] Your Python Regex Pattern Doesn’t Match? Try This! xSicKxBot 0 653 05-27-2022, 01:54 AM
Last Post: xSicKxBot
  [Tut] Python One Line Regex Match xSicKxBot 0 680 08-12-2020, 03:43 PM
Last Post: xSicKxBot
  [Tut] How to Match an Exact Word in Python Regex? (Answer: Don’t) xSicKxBot 0 890 03-08-2020, 02:43 PM
Last Post: xSicKxBot
  [Tut] Python Character Set [Regex Tutorial] xSicKxBot 0 924 02-19-2020, 08:47 AM
Last Post: xSicKxBot
  [Tut] Python Character Set [Regex Tutorial] xSicKxBot 0 952 02-18-2020, 09:12 PM
Last Post: xSicKxBot
  [Tut] Python Regex Match xSicKxBot 0 1,065 01-22-2020, 07:35 AM
Last Post: xSicKxBot

Forum Jump:


Users browsing this thread:
1 Guest(s)

Forum software by © MyBB Theme © iAndrew 2016