• python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Facebook Twitter Instagram
Devs Fixed
  • python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Devs Fixed
Home ยป Resolved: python-regex : change the desired word

Resolved: python-regex : change the desired word

0
By Isaac Tonny on 17/06/2022 Issue
Share
Facebook Twitter LinkedIn

Question:

I use the search functionre.finditer() and get span as 1 tuple

I’m trying a function that can handle any input but I can’t seem to get it to work
If you really need to use re.finditer, then you need to cut-and-paste the string yourself. You also want to reverse the matches, because the indices for later matches will get messed up with earlier edits (“cutting the branch you are sitting on”).
To only switch the items you want, you either make a more precise regex, or find a different criterion to do so. For example, to only replace the second one, which is capitalised, you could search for "Help" instead of "help", and not specify the re.I flag. Or you could search for (?<=help.*)help with re.I (a “help” that is preceded by another “help”). Or you could use re.sub with a replacer function and a nonlocal counter, so that you only replace the second match. Or you could use re.finditer to find a list of matches, extract the second one, and then perform the cut-and-paste from the first example.

‘extract the second one’ this is exactly my problem. How can i extract it from group of finditer?


If you have better answer, please add a comment about this, thank you!

python regex
Share. Facebook Twitter LinkedIn

Related Posts

Resolved: How to convert Java bytecode to Webassembly using CheerpJ compiler

24/03/2023

Resolved: Is pandas groupby() function always produce a DataFrame with the same order respect to the column we group by?

24/03/2023

Resolved: Kivy widget hierarchy not behaving as expected

24/03/2023

Leave A Reply

© 2023 DEVSFIX.COM

Type above and press Enter to search. Press Esc to cancel.