Python Forum
Replace changing string including uppercase character with lowercase character
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Replace changing string including uppercase character with lowercase character
#1
Hello Smile

My input lines :
<p>Material blabla ;<lb/>Dimension blabla ;<lb/></p>
<p>Sideral blublu ;<lb/>Sticky Gonzalez blibli;<lb/>Reductio ad absurdum bloblo</p>
For each "<lb/>" followed by an uppercase character, I want it replaced with a white space + same character but lowercase.

My wished output :
<p>material blabla ; dimension blabla ;<lb/></p>
<p>Sideral blublu ; sticky Gonzalez blibli; reductio ad absurdum bloblo</p>
I made several re.sub trials with unsuccessful regular expressions. I also read several things about it : it seems not so easy for a beginner in programming like me. So, if you have not an easy solution, please tell me what I should learn first before understanding it.

Thank you Smile
Reply
#2
show what you've tried.
We'll be happy to help, but will not write the code for you.
Reply
#3
Did I in fact try something?
I wanted to do something like this :
import re
list = ['<p>Material blabla ;<lb/>Dimension blabla ;<lb/></p>','<p>Sideral blublu ;<lb/>Sticky Gonzalez blibli;<lb/>Reductio ad absurdum bloblo</p>']
for member in list:
    output=re.sub(r"<lb/>\something-to-match-next-string-with-uppercase"," identified-string-with-lowercase",member)
... but it seems there is no regular expression for this.
Reply
#4
In a regex, [A-Z] will match an uppercase character.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
It is true !
New code, then :
list = ['<p>Material blabla ;<lb/>Dimension blabla ;<lb/></p>','<p>Sideral blublu ;<lb/>Sticky Gonzalez blibli;<lb/>Reductio ad absurdum bloblo</p>']
import re
for member in list:
    output=re.sub(r"<lb/>[A-Z]"," [a-z]",member)
    print(output)
Output :
<p>Material blabla ; [a-z]imension blabla ;<lb/></p>
<p>Sideral blublu ; [a-z]ticky Gonzalez blibli; [a-z]eductio ad absurdum bloblo</p>
So, identification is ok, replacement no.
Should my code do something like this :
1. Identify the character to be replaced - OK
2. Place this character in a variable
3. Lowercase it.
4. Put it back to the right place in the line.
?
Blush
Reply
#6
You can pass a callable to re.sub. This callable should take a regexp match object and return a string. The latter is used
to replace substrings that were found.

I think, you could adopt the following code for you needs:

def match_processor(m):
    print("Hey, I am match processor... ")
    print("Regexp engine found a substring: ", m.string[slice(*m.span())])
    print("This substring should be replaced with another one ... and I can do this!")
    replacement = '-' + m.string[slice(*m.span())] + '-'
    return replacement
    
list = ['<p>Material blabla ;<lb/>Dimension blabla ;<lb/></p>','<p>Sideral blublu ;<lb/>Sticky Gonzalez blibli;<lb/>Reductio ad absurdum bloblo</p>']
import re
for member in list:
    output = re.sub(r"<lb/>\s?[A-Z]", match_processor, member)
    print(output)
Reply
#7
Scidam,

I am sincerely grateful for your effort !
As a Beotian, I simply copy-pasted your code and ran it, unfortunately with no different result:
<p>Material blabla ; [a-z]imension blabla ;<lb/></p>
<p>Sideral blublu ; [a-z]ticky Gonzalez blibli; [a-z]eductio ad absurdum bloblo</p>
However, your proposal reminds me about "definition of callable functions" and this is good.
In your code, it is like the defined function is then not called... Or I miss something?

Cheers Smile
Reply
#8
(Mar-24-2019, 08:07 AM)silfer Wrote: In your code, it is like the defined function is then not called... Or I miss something?

match_processor function is called (by re.sub) each time when re.sub finds a substring that matches specified conditions (regexp).
Reply
#9
Sorry, scidam,
Your code produces a result. The got lines are :
<p>Material blabla ;-<lb/>D-imension blabla ;<lb/></p>
<p>Sideral blublu ;-<lb/>S-ticky Gonzalez blibli;-<lb/>R-eductio ad absurdum bloblo</p>
But the needed lines are :
<p>Material blabla ; dimension blabla ;<lb/></p>
<p>Sideral blublu ; sticky Gonzalez blibli; reductio ad absurdum bloblo</p>
The difficult task seems to be (example: first line) :
- Take got value "<lb/>D" and replace it with " d"
Reply
#10
I didn't post exact solution on purpose. This task seems to be an assignment, so, it would be better, if you try to modify the match_processor function for your needs by yourself. In this case you might ever use a lambda function instead of match_processor since string replacement requires one line of code only.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to replace on char with another in a string? korenron 3 136 Dec-03-2020, 07:37 AM
Last Post: korenron
  PIL PILLOW clear an character area Chuckv 5 182 Nov-19-2020, 07:37 AM
Last Post: DPaul
  Error with non-ASCII character? giomach 10 398 Nov-09-2020, 01:13 PM
Last Post: giomach
  Including modules in Python using sys.path.append JoeDainton123 1 355 Aug-24-2020, 04:51 AM
Last Post: millpond
  Iterating over word vs. character Mark17 6 337 Aug-11-2020, 04:56 PM
Last Post: Mark17
  Including a Variable In the HTML Tags When Sending An Email JoeDainton123 0 235 Aug-08-2020, 03:11 AM
Last Post: JoeDainton123
  SyntaxError: unexpected character after line continuation character siteshkumar 2 365 Jul-13-2020, 07:05 PM
Last Post: snippsat
  Remove from end of string up to and including some character lbtdne 2 487 May-17-2020, 09:24 AM
Last Post: menator01
  Python uppercase conversion conditions Jaypeng 7 555 Apr-29-2020, 11:24 AM
Last Post: jefsummers
  Help ANSI character and python... sid4g 2 455 Mar-19-2020, 06:08 PM
Last Post: sid4g

Forum Jump:

User Panel Messages

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