Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6116355
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T15:11:49+00:00 2026-05-23T15:11:49+00:00

So, I’ve seen this type thing on several occasions, but I’ve never been able

  • 0

So, I’ve seen this type thing on several occasions, but I’ve never been able to find out anything about it. Can someone please explain what these “r’ foo(*)'” things are and link me to the python docs for them? I think they have to do with finding key words in strings. Am I right?

(r'why (.*) i (.*)\?', 
( "You%1%2?", 
"Perhaps you only think you%1%2")), 

(r'why (.*) you(.*)\?', 
( "Why%1 you%2?", 
"%2 I%1", 
"Are you sure I%2?")), 

(r'why (.*)\?', 
( "I cannot tell you why%1.", 
"Why do you think %1?" )), 

This particular segment of code is from the nltk.chat.zen module.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T15:11:49+00:00Added an answer on May 23, 2026 at 3:11 pm

    Those appear to be regular expressions. Regular expressions let you search for patterns in strings in a fairly powerful and sophisticated way. They can be a bit cryptic though. These appear to be the basis of some kind of primitive chatterbot.

    >>> import re
    >>> regex, phrases = (r'why (.*) i (.*)\?', 
    ... ( "You %s %s?", 
    ... "Perhaps you only think you %s %s"))
    >>> phrase1, phrase2 = phrases
    >>> groups = re.search(regex, 'why am i dumb?')
    >>> phrase1 % (groups.group(1), groups.group(2))
    'You am dumb?'
    >>> phrase2 % (groups.group(1), groups.group(2))
    'Perhaps you only think you am dumb'
    

    Ah, and perhaps you were specifically wondering what r'blahblah' means. Well, as others have explained, the r just makes it a raw string — Python doesn’t do certain kinds of processing on it, which makes REs easier to read.


    Here’s something closer to what I think the original intent was for these strings. They don’t seem to be using standard Python group substitution syntax, so I assume they must be using a custom substitution function for this — perhaps one sophisticated enough to handle converting the verb form! You can see the basic idea here.

    >>> import re
    >>> regex, phrases = (r'why (.*) i (.*)\?',
    ... ( r'You \g<1> \g<2>?',
    ...   r'Perhaps you only think you \g<1> \g<2>.'))
    >>> phrase1, phrase2 = phrases
    >>> re.sub(regex, phrase1, 'why am i dumb?')
    'You am dumb?'
    >>> re.sub(regex, phrase2, 'why am i dumb?')
    'Perhaps you only think you am dumb.'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Seemingly simple, but I cannot find anything relevant on the web. What is the
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I am reading a book about Javascript and jQuery and using one of the
I have this code to decode numeric html entities to the UTF8 equivalent character.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.