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

  • Home
  • SEARCH
  • 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 6815185
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:46:37+00:00 2026-05-26T20:46:37+00:00

I’ve been trying to validate an inputted string ( sys argv[1] in this case).

  • 0

I’ve been trying to validate an inputted string (sys argv[1] in this case). I need to create a script that goes through a log file and matches the entries for source and destination ip with any argument input with the script. The kinds of valid inputs are either

  • an IP or partial ip
  • “any”(string which means all ip addresses in a given column).

So far I have the following code. Whenever I run the script in bash along with an argument (e.g any random number or word/alphabets etc) I get errors. Please let me know how I can fix them. Really appreciate a way to validate input against the IP address reg ex and the word any.

#!/usr/bin/python

import sys,re

def ipcheck(ip):
    #raw patterns for "any" and "IP":
    ippattern = '([1-2]?[0-9]?[0-9]\.){1,3}([1-2]?[0-9]?[0-9])?'
    anypattern = any
    #Compiled patterns
    cippattern = re.compile(ippattern)
    canypattern = re.compile(any)
    #creating global variables for call outside function    
    global matchip
    global matchany
    #matching the compiled pattern 
    matchip = cippattern.match(ip)
    matchany = canypattern.match(ip)

new = sys.argv[1]
snew = str(new)
print type(snew)
ipcheck(new)

Also I tried to do it this way but it kept giving me errors, is it possible to pass 2 arguments to an if loop via the “OR |” operator? How would I do it this way?[/b]

#if (matchip | matchany) :  
    #print "the ip address is valid"
#else:
    #print "Invalid Destination IP"


Error
========================

user@bt:/home# ./ipregex.py a
<type 'str'>
Traceback (most recent call last):
File "./ipregex.py", line 21, in <module>
ipcheck(new)
File "./ipregex.py", line 15, in ipcheck
matchany = re.match(anypattern,ip)
File "/usr/lib/python2.5/re.py", line 137, in match
return _compile(pattern, flags).match(string)
File "/usr/lib/python2.5/re.py", line 237, in _compile
raise TypeError, "first argument must be string or compiled pattern"
TypeError: first argument must be string or compiled pattern

==========================================================

EDIT

I was trying to match the IP without compiling the regex. So I modified the script to do so. This resulted in the error:

Error

user@bt:/home# ./ipregex.py a
<type 'str'>
Traceback (most recent call last):
File "./ipregex.py", line 21, in <module>
ipcheck(new)
File "./ipregex.py", line 15, in ipcheck
matchany = anypattern.match(ip)
AttributeError: 'builtin_function_or_method' object has no attribute 'match'

==========================================================

EDIT#2

I was able to reproduce my error in a simpler code version. What the heck am i doing wrong??????

#!/usr/bin/python

import sys
import re

def ipcheck(ip):
    anypattern = any
    cpattern = re.compile(anypattern)
    global matchany
    matchany = cpattern.match(ip)
    if matchany:
            print "ip match: %s" % matchany.group()
new = sys.argv[1]
ipcheck(new)

ERROR

user@bt:/home# ./test.py any
Traceback (most recent call last):
File "./test.py", line 14, in <module>
ipcheck(new)
File "./test.py", line 8, in ipcheck
cpattern = re.compile(anypattern)
File "/usr/lib/python2.5/re.py", line 188, in compile
return _compile(pattern, flags)
File "/usr/lib/python2.5/re.py", line 237, in _compile
raise TypeError, "first argument must be string or compiled pattern"
TypeError: first argument must be string or compiled pattern
  • 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-26T20:46:38+00:00Added an answer on May 26, 2026 at 8:46 pm

    When you use re.compile you call the match function on the compiled object: ippattern.match(ip). Also, to get to matched ip from a MatchObject, use MatchObject.group(). Fixed up your example some and it should now do what you need:

    #!/usr/bin/python
    
    import sys
    import re
    
    def ipcheck(ip):
        ippattern_str = '(([1-2]?[\d]{0,2}\.){1,3}([1-2]?[\d]{0,2})|any)'
    
        ippattern = re.compile(ippattern_str)
        # ippattern is now used to call match, passing only the ip string
        matchip = ippattern.match(ip)
        if matchip:
            print "ip match: %s" % matchip.group()
    
    if len(sys.argv) > 1:
        ipcheck(sys.argv[1])
    

    Some results:

    [ 19:46 jon@hozbox ~/SO/python ]$ ./new.py 100.
    ip match: 100.
    [ 19:46 jon@hozbox ~/SO/python ]$ ./new.py 100.1.
    ip match: 100.1.
    [ 19:46 jon@hozbox ~/SO/python ]$ ./new.py 100.1.55.
    ip match: 100.1.55.
    [ 19:46 jon@hozbox ~/SO/python ]$ ./new.py 100.1.55.255
    ip match: 100.1.55.255
    [ 19:47 jon@hozbox ~/SO/python ]$ ./new.py any
    ip match: any
    [ 19:47 jon@hozbox ~/SO/python ]$ ./new.py foo
    [ 19:47 jon@hozbox ~/SO/python ]$ 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
For some reason, after submitting a string like this Jack’s Spindle from a text
Basically, what I'm trying to create is a page of div tags, each has
I've got a string that has curly quotes in it. I'd like to replace
Does anyone know how can I replace this 2 symbol below from the string
I need a function that will clean a strings' special characters. I do NOT
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I have a jquery bug and I've been looking for hours now, I can't

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.