Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Regular Expressions
#1
Assume that a poem is given. Write the regular expressions for the following:
If the pattern has characters 'ai' or 'hi', replace the next three characters with *\*.
poem ='''
If I can stop one heart from breaking,
I shall not live in vain;
If I can ease one life the aching,
Or cool one pain,
Or help one fainting robin
Unto his nest again,
I shall not live in vain.'''
Reply
#2
What have you tried? We're not big on writing code for people here, but we would be happy to help you fix your code when you run into problems. When you do run into problems, please post your code in Python tags, and clearly explain the problem you are having, including the full text of any errors.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
See, I was trying this code:
c = poem
a = c.split(" ")

for i in range(0,len(a)):
    laura = 0
    for j in range(0,len(a[i])):
        if len(a[i])>6:
            laura = 0
        elif a[i][j] == "\n":
                laura = 1
    if len(a[i])>=6 and laura == 0:
        list_=list(a[i])
        for j in range(0,len(list_)):
            if list_[j] =="a" and list_[j+1] =="i":
                list_[j+2] = "*"
                list_[j+3] = "\\"
                list_[j+4] = "*"
        temp=""
        for j in list_:
            temp+=j
        a[i] = temp
        list__=list(a[i])
        for j in range(0,len(list__)):
            if list__[j] =="h" and list__[j+1] =="i":
                list__[j+2] = "*"
                list__[j+3] = "\\"
                list__[j+4] = "*"
        temp1=""
        for j in list__:
            temp1+=j
        a[i] = temp1
b =""
for i in range(0,len(a)):
    b+=a[i] +" "
print(b)
I am getting this Output:
Output:
If I can stop one heart from breaking, I shall not live in vai*\*If I can ease one life the achi*\* Or cool one pai*\*Or help one fai*\*ng robin Unto his nest agai*\*I shall not live in vain.
But the real output is
Output:
If I can stop one heart from breaking, I shall not live in vain; If I can ease one life the achi*\* Or cool one pain, Or help one fai*\*ng robin Unto hi*\*est again, I shall not live in vain.
Reply
#4
Your assignment says to use a regular expression, and you have failed to do that. You should read up on the re module, it will make the task so much simpler.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
Without regex (kiss):
poem.replace('ai', '*/*').replace('hi', '*/*')
A regex, which will detect ai and hi:
r'ai|hi'
You can use re.sub for replacement.
To construct or check a regex, you should visit this site:
https://regex101.com/
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Use or raw string on regular expressions Zaya_pool 5 517 May-09-2024, 06:10 PM
Last Post: Zaya_pool
Information Do regular expressions still need raw strings? bobmon 3 497 May-03-2024, 09:05 AM
Last Post: rishika24
  Recursive regular expressions in Python risu252 2 1,705 Jul-25-2023, 12:59 PM
Last Post: risu252
Sad Regular Expressions - so close yet so far bigpapa 5 1,198 May-03-2023, 08:18 AM
Last Post: bowlofred
  Having trouble with regular expressions mikla 3 2,786 Mar-16-2021, 03:44 PM
Last Post: bowlofred
  Regular Expressions pprod 4 3,284 Nov-13-2020, 07:45 AM
Last Post: pprod
  Format phonenumbers - regular expressions Viking 2 2,053 May-11-2020, 07:27 PM
Last Post: Viking
  regular expressions in openpyxl. format picnic 0 2,592 Mar-28-2020, 09:47 PM
Last Post: picnic
  Unexpected (?) result with regular expressions guraknugen 2 2,357 Jan-18-2020, 02:33 PM
Last Post: guraknugen
  Strange output with regular expressions newbieAuggie2019 1 2,036 Nov-04-2019, 07:06 PM
Last Post: newbieAuggie2019

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020