I'm trying to check if a subString exists in a string using regular expression.
RE : re_string_literal = '^"[a-zA-Z0-9_ ]+"$'
The thing is, I don't want to match any substring. I'm reading data from a file:
Now one of the lines have this text:
cout<<"Hello"<<endl;
I just want to check if there's a string inside the line and if yes, store it in a list.
I have tried the re.match method but it only works if we have to match a pattern, but in this case, I just want to check if a string exists or not, if yes, store it somewhere.
re_string_lit = '^"[a-zA-Z0-9_ ]+"$'
text = 'cout<<"Hello World!"<<endl;'
re.match(re_string_lit,text)
It doesn't output anything.
In simple words, I just want to extract everything inside ""
re.matchworks just fine for this, but you have not shown any code. Provide a minimal reproducible example.if s.strip():re.matchonly matches from the start of the string.re.searchwill look everywhere...!in the input string, which is not part of your regular expression. You might want to use[^"]rather than trying to enumerate all allowed characters, but then you need to address the issue of a string like"foo\"bar".