👻 Check our latest review to choose the best laptop for Machine Learning engineers and Deep learning tasks!
I need some help on declaring a regex. My inputs are like the following:
this is a paragraph with<[1> in between</[1> and then there are cases ... where the<[99> number ranges from 1-100</[99>.
and there are many other lines in the txt files
with<[3> such tags </[3>
The required output is:
this is a paragraph with in between and then there are cases ... where the number ranges from 1-100.
and there are many other lines in the txt files
with such tags
I"ve tried this:
#!/usr/bin/python
import os, sys, re, glob
for infile in glob.glob(os.path.join(os.getcwd(), "*.txt")):
for line in reader:
line2 = line.replace("<[1> ", "")
line = line2.replace("</[1> ", "")
line2 = line.replace("<[1>", "")
line = line2.replace("</[1>", "")
print line
I"ve also tried this (but it seems like I"m using the wrong regex syntax):
line2 = line.replace("<[*> ", "")
line = line2.replace("</[*> ", "")
line2 = line.replace("<[*>", "")
line = line2.replace("</[*>", "")
I dont want to hard-code the replace
from 1 to 99.
👻 Read also: what is the best laptop for engineering students?
We hope this article has helped you to resolve the problem. Apart from How to input a regex in string.replace?, check other code Python module-related topics.
Want to excel in Python? See our review of the best Python online courses 2023. If you are interested in Data Science, check also how to learn programming in R.
By the way, this material is also available in other languages:
- Italiano How to input a regex in string.replace?
- Deutsch How to input a regex in string.replace?
- Français How to input a regex in string.replace?
- Español How to input a regex in string.replace?
- Türk How to input a regex in string.replace?
- Русский How to input a regex in string.replace?
- Português How to input a regex in string.replace?
- Polski How to input a regex in string.replace?
- Nederlandse How to input a regex in string.replace?
- 中文 How to input a regex in string.replace?
- 한국어 How to input a regex in string.replace?
- 日本語 How to input a regex in string.replace?
- हिन्दी How to input a regex in string.replace?
London | 2023-03-29
Simply put and clear. Thank you for sharing. How to input a regex in string.replace? and other issues with glob Python module was always my weak point 😁. I just hope that will not emerge anymore
Milan | 2023-03-29
glob Python module is always a bit confusing 😭 How to input a regex in string.replace? is not the only problem I encountered. Checked yesterday, it works!
Warsaw | 2023-03-29
I was preparing for my coding interview, thanks for clarifying this - How to input a regex in string.replace? in Python is not the simplest one. Will use it in my bachelor thesis