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 1100415
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T00:52:39+00:00 2026-05-17T00:52:39+00:00

I am new to python (and this site); I am trying to write a

  • 0

I am new to python (and this site); I am trying to write a script that will use a regular expression to search through a given file to find a name. I have to print out the different ways the name was capitalized and how many times the name was found. My current code will just print out my first flag and then hang. I don’t know if my for loop or my reg exp is wrong. Thanks for your time!

import re
import sys
if __name__ == '__main__':
    print "flag"
    for line in sys.stdin:
        print(line) 
        name_count = re.search("[snyder]", line, re.I)
        variation = set(re.search(r"([snyder])", line, re.I))
    print "flag2"
    print len(name_count), variation
  • 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-17T00:52:39+00:00Added an answer on May 17, 2026 at 12:52 am

    First I would say to take a look at this thread for a bit of information about reading from stdin (if that’s really what you want to do).

    Second, I would consider just opening the file instead of reading from sys.stdin, either using a library like fileinput or a with statement or other file handle.

    Next, I would add that your regular expression probably isn’t going to do what you expect it to. The expression [snyder] is a character class, which will match one repetition of any character in the class. In other words, this will match the individual letters s, n, y, d, e, or r. If you want to match the literal string snyder then you should just use that as your expression: re.search("snyder", line, re.I). Or, if you don’t want substring matches (cases where snyder might appear within another string), you can try the regex \bsnyder\b.


    Edit re: your comment – Two things I’ll point out here:

    1) While [s][n][y][d][e][r] is semantically equivalent to snyder, you might want to consider using the latter for the sake of readability. A character class of one character is equivalent to that one character alone (as long as it’s properly escaped and so forth if necessary). Yours will work, so that’s just a suggestion/heads-up.

    2) Try using re.findall() in place of re.search(). I think you’ll get what you want with something like:

    variations = []
    for line in fileinput.input():
        found = re.findall(r"""snyder""", line, re.I)
        if len(found) > 0:
            variations += found
    var_set = set(variations)
    print var_set
    print len(var_set)
    

    An example of what this will do:

    >>> print sl 
    ['blah', 'blah', 'what', 'is', 'this', 'BLAh', 'some', 'random', 'bLah', 'text', 'a longer BlaH string', 'a BLAH string with blAH two']
    >>> li = []
    >>> for line in sl:
    ...   m = re.findall("blah", line, re.I)
    ...   if len(m) > 0:
    ...     li += m
    ... 
    >>> 
    >>> print li   #Contains all matches
    ['blah', 'blah', 'BLAh', 'bLah', 'BlaH', 'BLAH', 'blAH']
    >>> st = set(li)
    >>> print st   #Contains only *unique* matches
    set(['bLah', 'BLAH', 'BLAh', 'BlaH', 'blah', 'blAH'])
    >>> print len(st)
    6
    >>> print len(li)
    7    #1 greater than len(st) because st drops a non-unique match
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i'm new to python , and trying to write a script in order to
I'm trying to write (what I thought would be) a simple bash script that
I am trying to use networkx with Python. When I run this program it
I'm new to programing, this is my first python-gtk applet and I'm trying to
I am new to both python and Aptana. I am trying to use the
New to Python, but I'm trying to...retrieve data from a site: import urllib.request response
I've been trying to create new blog posts on my site using python and
I'm pretty new to Python and Scrapy and this site has been an invaluable
Very new to python and can't understand why this isn't working. I have a
I'm pretty new at python and I was wondering if this: def func(self, foo):

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.