Wirte a regular expression pattern to match all words ending in s.

This is what I got but seems like im thinking to hard on it, plus of course its not working lol Please help! :)

import re
str = "Subs are everwhere"
print (str)
matchObj = re.match(".*?s ",str,re.L|re.I)
if matchObj:
   print ("matchObj.group() : ", matchObj.group())
else:
   print ("none found")

Recommended Answers

All 2 Replies

Something like this,look into findall off re module.
Dont use str as variable,it`s a builtin key word.

>>> str
<type 'str'>
>>> help(str)
Help on class str in module __builtin__:
>>> import re
>>> help(re.findall)
Help on function findall in module re:

findall(pattern, string, flags=0)
    Return a list of all non-overlapping matches in the string.
    
    If one or more groups are present in the pattern, return a
    list of groups; this will be a list of tuples if the pattern
    has more than one group.
    
    Empty matches are included in the result.
import re

text = '''\
Subs cars are test driving in space,and many pigs are flying.
'''

test_match = re.findall(r'\w+[s]\b', text)
print test_match
#->['Subs', 'cars', 'pigs']

Thank you so much!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.