str.replace()
v2|v3 does not recognize regular expressions.
To perform a substitution using a regular expression, use re.sub()
v2|v3.
For example:
import re line = re.sub( r"(?i)^.*interfaceOpDataFile.*$", "interfaceOpDataFile %s" % fileIn, line )
In a loop, it would be better to compile the regular expression first:
import re regex = re.compile(r"^.*interfaceOpDataFile.*$", re.IGNORECASE) for line in some_file: line = regex.sub("interfaceOpDataFile %s" % fileIn, line) # do something with the updated line
Related Posts:
- Python string.replace regular expression
- Python regex AttributeError: ‘NoneType’ object has no attribute ‘group’
- python re.split() to split by spaces, commas, and periods, but not in cases like 1,000 or 1.50
- python re.split() to split by spaces, commas, and periods, but not in cases like 1,000 or 1.50
- Split string on whitespace in Python
- Grep and Python
- What is the grep equivalent in Python?
- Python re.split() vs split()
- python regular expression “\1”
- OR condition in Regex
- Using or ‘|’ in regex [duplicate]
- Renaming column names in Pandas
- What is a Context Free Grammar?
- python .replace() regex [duplicate]
- Replace ‘ with \’ in a string
- What does \’.- mean in a Regular Expression
- How can I exclude one word with grep?
- How can I validate an email address using a regular expression?
- Regular expression to match a line that doesn’t contain a word
- TypeError: ‘float’ object is not subscriptable
- TypeError: ‘float’ object is not subscriptable
- Difference between \b and \B in regex
- Regex: ignore case sensitivity
- What does the regex \S mean in JavaScript?
- whitespace in regular expression
- How can I validate an email address using a regular expression?
- Regex: ignore case sensitivity
- Regular Expression for alphanumeric and underscores
- What is the difference between the regular expressions [^\d\s] and [\D\S]
- Regex: AttributeError: ‘NoneType’ object has no attribute ‘groups’
- Regular Expression for alphanumeric and underscores
- re.sub erroring with “Expected string or bytes-like object”
- grep –ignore-case –only
- Regex AND operator
- Regex to test if string begins with http:// or https://
- Regular expression negative lookahead
- Regular Expression to match string starting with a specific word
- Regular Expressions: Is there an AND operator?
- Writing a very simple lexical analyser in C++
- Regular expression to match standard 10 digit phone number
- Finding and replacing elements in a list
- Regular Expression.how to add optional character at end of regex
- Regular Expression with wildcards to match any character
- What is a non-capturing group in regular expressions?
- How to find all occurrences of a substring?
- Does “\d” in regex mean a digit?
- How to validate an email address in PHP
- What is a non-capturing group in regular expressions?
- JavaScript replace/regex
- Replace values in list using Python
- Regular expression to match standard 10 digit phone number
- How to use Regular Expressions (Regex) in Microsoft Excel both in-cell and loops
- how to replace quotation marks with \”
- Remove all special characters, punctuation and spaces from string
- This can be done without regex:
- How to use Regular Expressions (Regex) in Microsoft Excel both in-cell and loops
- Symbol for any number of any characters in regex?
- Best way to replace multiple characters in a string?
- Regex – Does not contain certain Characters
- How to replace ” \ ” with ” \\ ” in java
- A regular expression to exclude a word/string
- Regex how to match an optional character
- How to validate an email address in JavaScript
- Using Bash regex match (=~) where regex includes quotes (” characters)
- Searching for UUIDs in text with regex
- Regular expression to match a word or its prefix
- How can I remove punctuation from input text in Java?
- Regular expression for exact match of a string
- Using the star sign in grep
- Regex optional group
- Regex for string contains?
- Question marks in regular expressions
- Difference between \w and \b regular expression meta characters
- Regular Expression – Validate Gmail addresses
- Remove all special characters, punctuation and spaces from string
- Best way to replace multiple characters in a string?
- Java; String replace (using regular expressions)?
- How can I remove punctuation from input text in Java?
- How can I output only captured groups with sed?
- How to negate specific word in regex?
- Replace and overwrite instead of appending
- javascript HTML from document.body.innerHTML
- Which regular expression operator means ‘Don’t’ match this character?
- How can I write a regex which matches non greedy?
- Regex Explanation ^.*$ [duplicate]
- Negative matching using grep (match lines that do not contain foo)
- How to exclude a specific string constant?
- How to replace a whole line with sed?
- Regex match everything after question mark?
- Validating IPv4 addresses with regexp
- How to validate phone number using PHP?
- Validate phone number with JavaScript
- Can a URL contain a semicolon and still be valid?
- How is \\n and \\\n interpreted by the expanded regular expression?
- Extract part of a regex match
- Regex to match only uppercase “words” with some exceptions
- Regular Expression to reformat a US phone number in Javascript
- Regex to match only letters
- Regex optional capturing group?
- Finding the complement of a DFA?