7 Replies - 2890 Views - Last Post: 10 June 2011 - 04:23 PM Rate Topic: -----

#1 jhar131  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 149
  • Joined: 03-February 10

Question about regular expressions

Posted 10 June 2011 - 01:26 PM

Hello all,

I'm attempting to write my first python program; translating a program I wrote in C++ last week. I have a question about re.match. I'm very new to regular expressions; as I have just switched to C++/Python on Fedora from Java on Windows. Anyhoo, here is my problematic function:
def ReadFile():
    status = 0
    report = open('/home/joshua/Cactus/simfactory/bin/Testbuild.txt', 'r')
    fail = re.search('(\bCommand\B)/>(\breturned\B)/>(\bexit\B)/>(\bstatus\B)/>',report)
    if fail:
      status = 2
      report.close()
    else:
      status == 1
      report.close()



There's a little more to it than that, but this is where the problem is. When I try to run, I get this error:
  File "/home/joshua/eclipse/workspace/PyShellTest/src/ShellTest.py", line 22, in ReadFile
    fail = re.search('(\bCommand\B)/>(\breturned\B)/>(\bexit\B)/>(\bstatus\B)/>',report)
  File "/usr/lib64/python2.7/re.py", line 142, in search
    return _compile(pattern, flags).search(string)
TypeError: expected string or buffer




I realize I'm probably doing something really elementary wrong, but my overall unfamiliarity with regular expressions is hurting me. Any push in the right direction would be greatly appreciated. Thanks in advance.

Josh


ETA: I have no idea why the ending \b's are capitalized. They are lowercase in the code and just switched on the board for some reason.

This post has been edited by jhar131: 10 June 2011 - 01:28 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Question about regular expressions

#2 Motoma  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 452
  • View blog
  • Posts: 797
  • Joined: 08-June 10

Re: Question about regular expressions

Posted 10 June 2011 - 01:29 PM

As it turns out, report is a file object, not a string. What you want to do is call report.read() to get the content of your file, then feed that result to re.search().
Was This Post Helpful? 2
  • +
  • -

#3 jhar131  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 149
  • Joined: 03-February 10

Re: Question about regular expressions

Posted 10 June 2011 - 01:34 PM

So something along the lines of:
hopeful = report.read()
fail = re.search('(\bCommand\B)/>(\breturned\B)/>(\bexit\B)/>(\bstatus\B)/>',hopeful)
if fail:



?
Was This Post Helpful? 0
  • +
  • -

#4 jhar131  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 149
  • Joined: 03-February 10

Re: Question about regular expressions

Posted 10 June 2011 - 01:40 PM

Holy crap my friend. You happen to be both blazingly fast and correct. :bananaman:

Positive rep for you. :genius:
Was This Post Helpful? 0
  • +
  • -

#5 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6107
  • View blog
  • Posts: 23,666
  • Joined: 23-August 08

Re: Question about regular expressions

Posted 10 June 2011 - 01:50 PM

Motoma is ALWAYS correct :)
Was This Post Helpful? 0
  • +
  • -

#6 jhar131  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 149
  • Joined: 03-February 10

Re: Question about regular expressions

Posted 10 June 2011 - 03:48 PM

Actually, now I have another issue with it. :sweatdrop: For whatever reason, it isn't changing the value that I have built around it. The test never fails. For example:
def ReadFile():
    status = 0;
    report = open('/home/joshua/Cactus/simfactory/bin/Testbuild.txt', 'r')
    hopeful = report.read()
    report.close()
    fail = re.search(r'Error',hopeful)
    if fail:
        status == 2
    else:
        status == 1
    print status
    if status == 2:
        SummaryPrint(status)
    else:
        SummaryPrint(status)
        Create()



When I inject this 'print status', the value of "status" is zero, no matter what. Any suggestions?
Was This Post Helpful? 0
  • +
  • -

#7 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5932
  • View blog
  • Posts: 12,855
  • Joined: 16-October 07

Re: Question about regular expressions

Posted 10 June 2011 - 04:15 PM

You're comparing, not assigning.

Try:
	if fail:
		status = 2
	else:
		status = 1


Was This Post Helpful? 1
  • +
  • -

#8 jhar131  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 149
  • Joined: 03-February 10

Re: Question about regular expressions

Posted 10 June 2011 - 04:23 PM

Wow. It's been a long day. Thanks for the help.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1