Python Forum
Use or raw string on regular expressions
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Use or raw string on regular expressions
#1
I am studying regular expressions in an introduction course and I don't completely understand the use of raw strings. I will ilustrate my question with the most basic example:
Quote:import re

text = "This is a text with a dot."
pattern = r"\."
match = re.findall(pattern, text)

print(match)
The use of '"r'' before the regular expresion "\." should impede the escape of the backslash before the ".", therefore searching for any character except a newline (use of ''.'' in regular expressions), but it doesn't; The expression is still interpreted as a escape, searching for the caracter ''.''.
Getting the same result that not using "r" before the regular expresion, Im confused of the use of "r".

Note: in other types of escapes as ''\n'', it does work as expected, impeding the escape and searching for the literal string "\n"
Reply


Messages In This Thread
Use or raw string on regular expressions - by Zaya_pool - May-09-2024, 04:08 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
Information Do regular expressions still need raw strings? bobmon 3 496 May-03-2024, 09:05 AM
Last Post: rishika24
  Recursive regular expressions in Python risu252 2 1,702 Jul-25-2023, 12:59 PM
Last Post: risu252
Sad Regular Expressions - so close yet so far bigpapa 5 1,198 May-03-2023, 08:18 AM
Last Post: bowlofred
  Having trouble with regular expressions mikla 3 2,786 Mar-16-2021, 03:44 PM
Last Post: bowlofred
  Regular expression: cannot find 1st number in a string Pavel_47 2 2,524 Jan-15-2021, 04:39 PM
Last Post: bowlofred
  Regular expression: return string, not list Pavel_47 3 2,640 Jan-14-2021, 11:49 AM
Last Post: Pavel_47
  Regular Expressions pprod 4 3,284 Nov-13-2020, 07:45 AM
Last Post: pprod
  simple f-string expressions to access a dictionary Skaperen 0 1,609 Jul-15-2020, 05:04 AM
Last Post: Skaperen
  Format phonenumbers - regular expressions Viking 2 2,053 May-11-2020, 07:27 PM
Last Post: Viking
  regular expressions in openpyxl. format picnic 0 2,592 Mar-28-2020, 09:47 PM
Last Post: picnic

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020