0

You can I find a substring between characters I want to find this character svm? I looked at Get string between two strings and Find string between two substrings. So this is my string ('svm', SVC()) And I want to find all between ' ' so the result should be svm or dct_test

import re 
dict_SupportVectorMachine = {
    "classifier": ('svm', SVC()),
    "parameters": {'svm__C':[0.001,0.01,0.1,10, 100],
                   'svm__gamma':[0.1,0.01],
                   'svm__kernel':['linear', 'sigmoid']}
}

string = dict_SupportVectorMachine['classifier']
string2 = ('dct_test', ThisCouldbeLongerAndShorter())
subStr = re.findall(r"('(.+?)',",string)
print(subStr)
[OUT]
error: missing ), unterminated subpattern at position 0
Test
  • 571
  • 13
  • 32
  • 1
    `)` is a special character in regex, you seemed to have an unclosed parenthesis or a parenthesis that isn't meant to be here. – aph Nov 27 '21 at 09:06
  • See my edited answer, you don't have at all to handle string manipulations – azro Nov 27 '21 at 11:41

2 Answers2

1

It seems that you missed a ) on the fourth line:

import re 
string = "('svm', SVC())"
string2 = "('dct_test', ThisCouldbeLongerAndShorter()))"
subStr = re.findall(r"('(.+?))',",string)
print(subStr)
Cardstdani
  • 4,999
  • 3
  • 12
  • 31
  • 1
    Thank you for the quick reply. I appreciate it, however I forgot to include my `dict_SupportVectorMachine `. So I get exactly the `('svm', SVC())` without the `""`. Can it be used to find it ? str(dict_SupportVectorMachine[...]) didn't work. – Test Nov 27 '21 at 09:10
1

What you have is a tuple of 2 elements : a str and a SVC instance, just get the first index

dict_SupportVectorMachine = {
    "classifier": ('svm', SVC()),
    "parameters": {}
}

classif = dict_SupportVectorMachine['classifier']
print(classif[0])  # svm

OLD answer due to different question

The parenthesis is a special char for building groups, for a real parenthesis yo need to escape it \(. Also use search and not findall here

import re

string = "('svm', SVC())"
print(re.findall(r"\('(.+?)',", string))  # ['svm']
print(re.search(r"\('(.+?)',", string).group(1))  # svm

string2 = "('dct_test', ThisCouldbeLongerAndShorter()))"
print(re.findall(r"\('(.+?)',", string2))  # ['dct_test']
print(re.search(r"\('(.+?)',", string2).group(1))  # dct_test
azro
  • 53,056
  • 7
  • 34
  • 70
  • Thank you for the quick reply. I appreciate it, however I forgot to include my `dict_SupportVectorMachine `. So I get exactly the `('svm', SVC())` without the `""`. Can it be used to find it ? str(dict_SupportVectorMachine[...]) didn't work. – Test Nov 27 '21 at 09:10
  • @Test what ?? a second ago your `string` contained a str object, now it is a tuple of : one string + one object, do `print(string[0])` and that's it – azro Nov 27 '21 at 09:22