0

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 ""

12
  • 1
    re.match works just fine for this, but you have not shown any code. Provide a minimal reproducible example. Commented Jan 8, 2021 at 16:48
  • 1
    What do you mean if a string exists or not? It is a string... To check if it's not empty you can do if s.strip(): Commented Jan 8, 2021 at 16:49
  • 2
    @chepner not accurate. re.match only matches from the start of the string. re.search will look everywhere... Commented Jan 8, 2021 at 16:52
  • 2
    One of your examples has a ! 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". Commented Jan 8, 2021 at 16:54
  • 2
    @QasimKhan What about quotes inside comments? Do you want actual C++ strings, or just anything that's surrounded by quotes? Commented Jan 8, 2021 at 17:01

2 Answers 2

1

If you just want to extract everything inside "" then string splitting would be much simpler way of doing things.

>>> a = 'something<<"actualString">>something,else'
>>> b = a.split('"')[1]
>>> b
'actualString'

The above example would only work for not more than 2 instances of double quotes ("), but you could make it work by iterating over every substring extracted using split method and applying a much simpler Regular Expression.

Sign up to request clarification or add additional context in comments.

Comments

0

This worked for me:


re.search('"(.+?)"', 'cout<<"Hello"<<endl')

4 Comments

"([^"]+)" is a bit more efficient than a lazy quantifier on .
@Tomerikoo Thankyou so much. Is there a way to access this substring after performing the search method?
Yes. it is stored in group 1. So, match = re.search('"(.+?)"', 'cout<<"Hello"<<endl') and the string is match.group(1). If you want it with the quotes, then it's group(0) or just group()
@Tomerikoo Thank you so much!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.