Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to do a case insensitive replace but match the case of the word to be replaced?

So far I've come up with the method below but my question is is there a shorter method out there that has the same result?

My Code :

input_str       = "myStrIngFullOfStUfFiWannAReplaCE_StUfFs"
replace_str     = "stuff"
replacer_str    = "banana"


print input_str
# prints: myStrIngFullOfStUfFiWannAReplaCE_StUfFs

if replace_str.lower() in input_str.lower():            # Check if even in the string
    begin_index = input_str.lower().find( replace_str )
    end_index   = begin_index + len( replace_str )

    replace_section = input_str[ begin_index : end_index ]

    case_list = []
    for char in replace_section:                        # Get cases of characters in the section to be replaced
        case_list.append( char.istitle() )

    while len( replacer_str ) > len(case_list):
        case_list += case_list

    sameCase_replacer_str   = ""                        # Set match the replacer string's case to the replace
    replacer_str            = replacer_str.lower()
    for index in range( len(replacer_str) ):
        char = replacer_str[ index ]
        case = case_list[ index ]
        if case == True:
            char = char.title()

        sameCase_replacer_str += char

    input_str = input_str.replace( replace_section , sameCase_replacer_str )

print input_str
# prints: myStrIngFullOfBaNaNAiWannAReplaCE_BaNaNAs
like image 594
Jay Avatar asked Feb 09 '12 10:02

Jay


People also ask

How do you replace a case sensitive String in Java?

A simpler solution is to use StringBuilder objects to search and replace text. It takes two: one that contains the text in lowercase version while the second contains the original version. The search is performed on the lowercase contents and the index detected will also replace the original text.

Is replace in python case sensitive?

Is the String replace function case sensitive? Yes, the replace function is case sensitive. That means, the word “this” has a different meaning to “This” or “THIS”. In the following example, a string is created with the different case letters, that is followed by using the Python replace string method.

Is replace case sensitive?

Replace() method allows you to easily replace a substring with another substring, or a character with another character, within the contents of a String object. This method is very handy, but it is always case-sensitive.

How to do a case insensitive replace all with JavaScript string?

To do a Case insensitive replace all with JavaScript string, we can use a regex to do the replacement. For instance, we can write: const str = 'This iS IIS'.replace (/is/ig, 'as'); console.log (str) We call replace on a string with the i and g flags to search for all instances of 'is' in a case insensitive manner.

How to use sed for case insensitive search and replace?

Let us see how to use sed for case insensitive search and replace under Linux or Unix-like systems including macOS. Replacing or substituting string is simple. The below example replaces the word wiwek with vivek in the file: # The g flag applies the replacement to all matches to the regexp, not just the first. #

How can I replace ignore case in a string?

My suggestion would be to create a custom function query named Text_ReplaceIgnoreCase, with the following code: Very nice solution Colin, as yours will also translate all substrings within a string. Just in case this is not what's needed, but the replacement shall only be performed on complete strings, one could use this function then:

How to replace string with new string case-insensitively in Python?

We can use python regression expression to do it. import re def replace (old, new, str, caseinsentive = False): if caseinsentive: return str.replace (old, new) else: return re.sub (re.escape (old), new, str, flags=re.IGNORECASE) In this function, if caseinsentive = False, this function will replace old string with new string case-insensitively.


3 Answers

I'd use something like this:

import re

def replacement_func(match, repl_pattern):
    match_str = match.group(0)
    repl = ''.join([r_char if m_char.islower() else r_char.upper()
                   for r_char, m_char in zip(repl_pattern, match_str)])
    repl += repl_pattern[len(match_str):]
    return repl

input_str = "myStrIngFullOfStUfFiWannAReplaCE_StUfFs"
print re.sub('stuff',
             lambda m: replacement_func(m, 'banana'),
             input_str, flags=re.I)

Example output:

myStrIngFullOfBaNaNaiWannAReplaCE_BaNaNas

Notes:

  • This handles the case in which the different matches have different upper/lower case combinations.
  • It's assumed that the replacement pattern is in lower case (that's very easy to change, anyway).
  • If the replacement pattern is longer than the match, the same case as in the pattern is used.
like image 50
jcollado Avatar answered Oct 23 '22 01:10

jcollado


You can pass flags=re.I to re.sub() to ignore-case

>>> input_str       = "myStrIngFullOfStUfFiWannAReplaCE_StUfFs"
>>> replace_str     = "stuff"
>>> replacer_str    = "banana"
>>> 
>>> import re
>>> from itertools import zip_longest
>>> tr = lambda x, y: ''.join([i[0].upper() if i[1] else i[0].lower() for i in zip_longest(y, [c.isupper() for c in x], fillvalue=(lambda : '' if len(x)>len(y) else x[-1].isupper())())])
>>> re.sub(replace_str, lambda m: tr(m.group(0), replacer_str), input_str, flags=re.I)
'myStrIngFullOfBaNaNaiWannAReplaCE_BaNaNas'
like image 40
kev Avatar answered Oct 23 '22 01:10

kev


From the code it's obvious that the case pattern for replacement is made from the match case pattern by repeating it over (so StufF -> BanaNA). Bearing this in mind I would first find case pattern for the whole string, and then bring the string to desired case:

def to_case(s, cmap):
    'returns string cased according to map'
    return ''.join([c.upper() if m else c for (c,m) in zip(s,cmap)])

input_str       = "myStrIngFullOfStUfFiWannAReplaCE_StUfFs"
replace_str     = "stuff"
replacer_str    = "banana"

case_map = [c.istitle() for c in input_str] # initial case map
input_str_lower = input_str.lower()    

while replace_str.lower() in input_str_lower:            # Check if even in the string
    ind = input_str_lower.find(replace_str)  # find index
    cases = [case_map[(ind + i % len(replace_str))] for i in range(len(replacer_str))] # replacement case pattern
    case_map = case_map[:ind] + cases + case_map[ind + len(replace_str):]
    input_str_lower = input_str_lower.replace(replace_str, replacer_str, 1)

print to_case(input_str_lower, case_map)
# prints: myStrIngFullOfBaNaNAiWannAReplaCE_BaNaNAs
like image 1
Andrey Sobolev Avatar answered Oct 23 '22 02:10

Andrey Sobolev