2

I want to do a case sensitive match from the text. In the below case i tried matching the "Ca.iNy" using re.search, I want to match where "C" should be in upper case and rest all characters might be in any of the cases. If it matches the case i want set a value to a variable.

I have taken the help form SO and implemented by checking whether the first letter is a capital or not and it worked fine for a single check.

s = "The details belong to (Ca.iNy.) in this case"
reg = re.compile("Ca.iny.", re.I)
reg.search(s).group().startswith("C").

However, I am not able to use it in an "if else loop". I tried the code below, but the search seems to be case-insensitive. Can anyone please let me?

import re

st = "The details belong to (Ca.iNy.) in this case"
Mval = ''

if re.search(r"C(?i)a.iny", st):
    Mval = "AAAAA"
elif re.search(r"(?i)Ky.", st):
    Mval = "BBBBB"
elif re.search(r"(?i)M.sa.", st):
    Mval = "CCCCC"
else:
    Mval = "DDDDD"

print Mval
D.Shawley
  • 58,213
  • 10
  • 98
  • 113
Fla-Hyd
  • 279
  • 7
  • 17
  • @Wooble. The answer which is there deals with only a single case. But problem here is during implementing with if else loop. Because if need to match around 50 cases using if else. it will not be fine if i compile all the patterns initially. – Fla-Hyd Feb 26 '13 at 16:42
  • @Wooble Can you please unmark from duplicate. so that any one of you can help me in implementing this? – Fla-Hyd Feb 26 '13 at 16:48
  • `if`/`else` is a) **not a loop** and b) **not relevant to the problem**. The problem is that you have switched from using a compiled regex with a `search` method to calling the module's `re.search`. This can be addressed in two ways: by **using separate compiled regexes in each place**, according to the exact way that you **already found** to solve the problem; or by telling the `re.search` to work case-insensitively - it can be passed the same `re.I` flag; see the linked duplicate for details. – Karl Knechtel Mar 07 '23 at 01:19

4 Answers4

0
import re

st = "The details belong to (Ca.iNy.) in this case"
Mval = ''

if re.search(r"C""(?i)a.iny", st):
    Mval = "AAAAA"
elif re.search(r"(?i)Ky.", st):
    Mval = "BBBBB"
elif re.search(r"(?i)M.sa.", st):
    Mval = "CCCCC"
else:
    Mval = "DDDDD"

print Mval
jeff
  • 1
0
import re
All = {"CA.ing": 3, "cA.aec": 10}
st = "The details belong to (Ca.inY.) in this case"
Mval = ''
class1 = r"[a-z][a-z].[a-z][a-z][a-z]"
class2 = r"[a-z][a-z][a-z][a-z][a-z][a-z]"  # For strings like alaska

if re.search(class1, st, flags=re.IGNORECASE):
    found_value = re.search(class1, flags=re.IGNORECASE).group()
    if found_value in All.keys():
        Mval = All[found_value]
elif re.search(class2, st):
    found_value = re.search(class2, st).group()
    if found_value in All.keys():
        Mval = All[found_value]

#This will return a KeyError if a string is present not in your dictionary
#Note : You can take care of the different case sensitive cases in the dictionary, by
# only including the proper cases
  • Is it possible to divide the expected groups into classifications? – user2112190 Feb 26 '13 at 18:50
  • like, some are 6 chars long, some have a "." separator"? This will help you to not need a crazy long list of if/else statements – user2112190 Feb 26 '13 at 18:50
  • Firstly thanks for your answers and help. i hope its difficult to have an expected group classification. – Fla-Hyd Feb 26 '13 at 19:04
  • Ok well, just suppose you were able to, I will upload one more answer that might help, with comments nearby that should help, and as a note, it is very important which cases are meant for case sensitivity and which are not – user2112190 Feb 26 '13 at 19:08
  • This should work i guess. i will let you know i case i need any help further. once again thanks for your answer. – Fla-Hyd Feb 26 '13 at 19:20
0
[mport re

reg = re.compile('([a-z]{2})\.[a-z]{3}',re.I)

def code(X,r):
    for ma in r.finditer(X):
        if ma.group(1)[0].upper()==ma.group(1)[0]:
            GV = 'OK'
        else:
            GV = '- bad match -'
        yield '  {!s:10}  {!s:^13}'.format(ma.group(), GV)

data = ('Ca.imo  Ca.IMo gggg Ca.iMo   CL.icv   cl.icv  cL.icv'
        'hhhh  ca.ghc  Rl.axp  bbb  Rl.AXp  nm.fgt')

print '\n'.join(res for res in code(data,reg))

result

  Ca.imo           OK      
  Ca.IMo           OK      
  Ca.iMo           OK      
  CL.icv           OK      
  cl.icv      - bad match -
  cL.icv      - bad match -
  ca.ghc      - bad match -
  Rl.axp           OK      
  Rl.AXp           OK      
  nm.fgt      - bad match -
eyquem
  • 26,771
  • 7
  • 38
  • 46
-1
import re

st = "The details belong to (Ca.iNy.) in this case"
Mval = ''

if re.search(r"C[a-z].[a-z][a-z]", st):   # Only change
    Mval = "AAAAA"
elif re.search(r"(?i)Ky.", st):
    Mval = "BBBBB"
elif re.search(r"(?i)M.sa.", st):
    Mval = "CCCCC"
else:
    Mval = "DDDDD"

print Mval
  • This answer would be a bit more useful if you included a little text. Try explaining what you did to solve the problem, and why. – Bryan Oakley Feb 26 '13 at 17:04
  • Sorry about that, the one with no comment was an accidental post. – user2112190 Feb 26 '13 at 17:05
  • This one, only has the [a-z].[a-z][a-z] placed instead of r"C""(?i)a.iny", st) , for readability, and to make the following letters lower-case – user2112190 Feb 26 '13 at 17:06
  • It is primitive, but the search moves on to the else at the end. – user2112190 Feb 26 '13 at 17:06
  • @user2112190 But consider a case where if it matches "Ca.iRo." i need to set Mval to "DDDDD" and if matched "Ca.IPl Vn.Lp" . i need to set Mval to "FFFFF" i need to keep such conditions around 50. – Fla-Hyd Feb 26 '13 at 18:17
  • sorry i keep pressing enter. if re.search(#pattern#): ....if re.search(#pattern#).match() in dictionary.keys(): ........Mval = dictionary[re.search(#pattern#).match()] # In other words, a dictionary with strings as keys and mvals as values, you would only need one if statement – user2112190 Feb 26 '13 at 18:31
  • @user2112190 My point is how can we do a case sensitive match in re.search? like "Ca.iNy" and "ca.iNy" are different. think suppose case i want to match a sentence "Freedom FIGHTER" where first word "F" is capital and the secondword is totally capital. – Fla-Hyd Feb 26 '13 at 18:35
  • the beginning of the page implied that you are always expecting the start of the string to be "C", is that not the case? the current loop ensures that only a string with a capital C is matched. – user2112190 Feb 26 '13 at 18:37
  • @user2112190 I have many such cases for example: Ca.imo, CL.icv, Ca.ghc, Rl.axp, Nm.fgt, ....... if it matches Ca.imo i want to set to "aaaa" if it matches "CL.icv" i want to set to "bbbb" – Fla-Hyd Feb 26 '13 at 18:39
  • to be more precise, here is what could work, STEP 1, use a re.search to return string with "Ca." at the start. STEP 2, search dictionary keys for all expected strings. STEP 3, if matched value is a key, set "mval" equal to the dictionary value – user2112190 Feb 26 '13 at 18:39
  • Well for something like this, there must be a unifying quality, here, it seems they are all 6 characters long with 2 letters in front, a ".", and three more letters – user2112190 Feb 26 '13 at 18:40
  • if that is the case, I will post one more answer which you may find helpful. Are all expected strings of that form? – user2112190 Feb 26 '13 at 18:41
  • @user2112190 no not really . there is a case where i need to match a word something like this "Alaska" where "A" should be capital – Fla-Hyd Feb 26 '13 at 18:43