Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
regex question
#4
(Jun-18-2022, 02:24 PM)DPaul Wrote: String.replace() is not "just" faster, it is 45 times faster than regex.
I most cases it dos not matter at all,if run one time will never feel the difference.
(Jun-18-2022, 02:24 PM)DPaul Wrote: Are you challenging me to beat regex in doing these replacements?
You can try i did a test just now and it did not look good with replace(need many calls).
Then a surprise need to this with more strings.
As you see regex has no problem as the pattern will work for many different scenarios,
with replace has to start from scratch on the new string.
import re

#target_str = "Jessa  Knows Testing    And Machine     Learning \t \n"
target_str = "Paul  Knows Testing  And   Machine Learning\n\t  \n"
res_str = re.sub(r"\s+", " ", target_str)
print(res_str)
Output:
Paul Knows Testing And Machine Learning
Reply


Messages In This Thread
regex question - by DPaul - Jun-18-2022, 09:20 AM
RE: regex question - by snippsat - Jun-18-2022, 11:34 AM
RE: regex question - by DPaul - Jun-18-2022, 02:24 PM
RE: regex question - by snippsat - Jun-18-2022, 03:21 PM
RE: regex question - by DPaul - Jun-18-2022, 05:31 PM
RE: regex question - by DPaul - Jun-19-2022, 06:42 AM
RE: regex question - by DPaul - Jun-19-2022, 08:37 AM

Forum Jump:

User Panel Messages

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