0

I want to write a program which checks if a string starting with a given number. Lets say, we have a string: line = "5p.m. its a lovely time for tea!" and then I read a number from user: num = raw_input() and want to check if my line starts with a number that user passed.

I want to do something like (here might be some mistakes):

s = compile(r"^[\<%\>]", num)
m = s.search(line)

or

s = compile(r"^[\<%\>]", num)
m = s.search(line)

and when user pass a 5, it will print "OK" or something like that when line starts with 5, or "Its not OK" when my line starts with a different number. Im very newbie in this whole regex thing, please, help :) I accept code both for C++ and Python ;)

2
  • do you want a c++ answer, or a python answer? you should not have two programming languages tagged unless it is relevant, i have seen from your comments you want a c++ answer, why did you tag this question as python? Commented Jan 24, 2013 at 10:48
  • I think it would be fair to us users to remove the C++ tag to solve for the potential confusion (however, I don't think a downvote is justified here). Commented Jan 24, 2013 at 11:11

5 Answers 5

5

Regex is overkill:

line = '5p.m. its a lovely time for tea!'
num = raw_input()  # integer input
if line.startswith(num):
    print 'OK'
else:
    print 'It\'s not OK'

If you need to use regex, you can do this:

if re.search('^%s' %num, line):  # checks if there is a match
    print 'OK'
else:
    print 'It\'s not OK'

The reason this works is because if there is a match, re.search will return an _sre.SRE_Match object will evaluate to True in a boolean context. If there is no match, it will return None will evaluates to False.

Sign up to request clarification or add additional context in comments.

1 Comment

yup, I know I can do this but unfortunately, I need to do it with regex :)
2

I don't know about c++ and python coding but regex that you want is just

^(\d).*

you can use http://regexpal.com/ to check result

PS. Sorry i try to post image but they don't allow so click the link to see https://i.sstatic.net/qwBjJ.png

1 Comment

^\d is sufficient to check. ^(\d) is sufficient for capturing the digit.
1
import re
number = 5
line = "5p.m. its a lovely time for tea!"
s = compile("^%d" % num)
m = s.search(line)

if m:
  print "time for tea"

2 Comments

Maybe You know how to do the same thing in C++ with boost?
@yak or try the regex include shipped with c++0x
0
import re

line = "5p.m. its a lovely time for tea!"

match_obj = re.search('^([0-9]+).*', line)

num = raw_input()

if num == match_obj.groups()[0]:

    print "OK"

else:

    print r"It's not ok"

Comments

0

I would echo the response above: regex is overkill for this problem. Use startswith as pointed out above.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.