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?
- Replace ‘ with \’ in a string
- 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
- Difference between \b and \B in regex
- 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
- Regex: AttributeError: ‘NoneType’ object has no attribute ‘groups’
- Regular Expression for alphanumeric and underscores
- re.sub erroring with “Expected string or bytes-like object”
- Regex to test if string begins with http:// or https://
- Regular expression negative lookahead
- Writing a very simple lexical analyser in C++
- Finding and replacing elements in a list
- What is a non-capturing group in regular expressions?
- What is a non-capturing group in regular expressions?
- How to use Regular Expressions (Regex) in Microsoft Excel both in-cell and loops
- This can be done without regex:
- Symbol for any number of any characters in regex?
- Best way to replace multiple characters in a string?
- How to replace ” \ ” with ” \\ ” in java
- A regular expression to exclude a word/string
- Regex how to match an optional character
- 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?
- Regex optional group
- Question marks in regular expressions
- Difference between \w and \b regular expression meta characters
- 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 output only captured groups with sed?
- How to negate specific word in regex?
- javascript HTML from document.body.innerHTML
- 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?
- Regex match everything after question mark?
- Validating IPv4 addresses with regexp
- Validate phone number with JavaScript
- 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 optional capturing group?
- RegEx to find two or more consecutive chars
- RE error: illegal byte sequence on Mac OS X
- Using replace() method in python by index
- How to remove parentheses from string [duplicate]
- Regex to match an optional ‘+’ symbol followed by any number of digits
- Regex Match all characters between two strings
- How to replace multiple substrings of a string?
- Regular expression to match a word or its prefix
- Regular expression for a string that does not start with a sequence
- What does this Django regular expression mean? `?P`
- Regular expression – starting and ending with a character string
- Regular expression to stop at first match
- /bb|[^b]{2}/ how does it work?
- How do I match any character across multiple lines in a regular expression?
- Regular expression to remove HTML tags from a string
- Regular expression to limit number of characters to 10
- My Vim replace with a regex is throwing a `E488: Trailing characters`
- Regex for “AND NOT” operation
- Python regex match space only
- Regular expression that matches valid IPv6 addresses
- Escaping Parentheses in Regex
- Case insensitive regular expression without re.compile?
- python’s re: return True if string contains regex pattern
- What is the best regular expression to check if a string is a valid URL?
- Regex empty string or email
- Regex to validate date formats dd/mm/YYYY, dd-mm-YYYY, dd.mm.YYYY, dd mmm YYYY, dd-mmm-YYYY, dd/mmm/YYYY, dd.mmm.YYYY with Leap Year Support
- Regex difference: (\w+)? and (\w*)
- Wrap element with in any number of elements with PHP
- 404 regular-expression advice needed
- How to use a variable as a className inside preg_match_all parameters?
- Adding span tags to post titles using regex
- Redirect non www to www using redirection plugin regex
- Restrict comments with regular expression
- Remove Custom HTML Posts Using Regex
- Regexes allow only [a-z][A-Z][0-9] and –
- Call Shortcode Attribute Value within another function
- How to fix old embeded iframes with WP-CLI search-replace