. in regex

Joined
Jul 20, 2023
Messages
60
Reaction score
2
when compiling a pattern in regex, the dot can mean any character at all except a new line right? How does it distinguish weather your looking for any character or your looking specifically a dot?
 
Joined
Jul 4, 2023
Messages
565
Reaction score
73
You need to precede it with a backslash \ , then this notation \. allows you to match an actual dot.
Python:
import re

text = "a.b a-b a b" # Example string

# Using unescaped dot: matches any character
matches_any_char = re.findall(r"a.b", text)

# Using escaped dot: matches only "a.b"
matches_literal_dot = re.findall(r"a\.b", text)

''' BTW,
    The string \"a\\.b\" in Python is interpreted as "a\.b", where:
    \" escapes the double quotes,
    \\. escapes \ before . (dot)
'''
print("Matches for \"a.b\":", matches_any_char)
print("Matches for \"a\\.b\":", matches_literal_dot)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,156
Messages
2,570,878
Members
47,404
Latest member
PerryRutt

Latest Threads

Top