0

I want to match the first and last quotation mark from the example code below using QRegExp:

echo "#!/bin/bash

VAR="Test"
Script content

" > $SCRIPT

I have tested several different expressions, the closest I have gotten so far is by using QRegExp("\"([^\"]*)\""), which only match two consecutive quotation marks on the same line (In this case "Test").

Can anyone help me with this?

eivindmu
  • 1
  • 2
  • 1
    Do you mean you want to match the whole text from the first `"` till the last `"`? `QRegExp("\".*\"")`? – Wiktor Stribiżew May 07 '18 at 13:15
  • Welcome to the site! Check out the [tour](https://stackoverflow.com/tour) and the [how-to-ask page](https://stackoverflow.com/help/how-to-ask) for more about asking questions that will attract quality answers. You can [edit your question](https://stackoverflow.com/posts/50215124/edit) to include more information. – cxw May 07 '18 at 13:38
  • See my edited question. Hope this made my problem clearer. – eivindmu May 08 '18 at 06:11

1 Answers1

0

I don't know anything about Qt, but from what I read, QRegExp is pretty limited in what it can do, and things like wildcard characters can be problematic. If possible, use QRegularExpression Class instead. I believe the following would do what you want, but probably not with QRegExp. It would work in Perl-like RegExp engines:

echo\s+["']((?:[^"']|["'](?!\s*>))+)["']

What this does is search for "echo" followed by at least one whitespace, then match either " or ' (I assume either could be used), followed by anything that is not a quotation mark or anything that is a quotation mark not followed by >, at least once and as many times as possible, and then it matches the closing quotation mark.

It's important to realize that the regex engine needs some way of distinguishing what is the starting quote and what is the ending. What I've assumed here is that the start quote is always preceded by echo and the end quote is always followed by >. You might have to tweak things if those assumptions are incorrect.

Jaifroid
  • 437
  • 3
  • 6
  • Sorry for my late reply, but by using regexr.com I found out that this expression does what I want: `["']((?:[^"']|["'](?!\s*>))+)["']` So I tried putting this in a QRegularExpression in the following way: `QRegularExpression("[\"]((?:[^\"]|[\"](?!\\s*>))+)[\"]")` The problem with this is that the regex seems to include the backslashes, which needs to be there to escape the quotation marks. Any ideas on how to solve this? – eivindmu May 29 '18 at 11:34