Regex - Match string between quotes (") but do not match (\") before the string - regex

I need to match a string that is in quotations, but make sure the first quotation is not escaped.
For example: First \"string\" is "Hello \"World\"!"
Should match only Hello \"World\"!
I am trying to modify (")(?:(?=(\\?))\2.)*?"
I tried adding [^\\"] to ("), and that kinda works, but it matches either only (") or every other letter that isn't (\") and I can't figure out a way to modify ([\\"]") to only match (") if it is not (\")
This is what I have so far ([^\\"]")(?:(?=(\\?))\2.)*?"
I've been trying to figure it out using these two pages, but still cannot get it.
Can Regex be used for this particular string manipulation?
RegEx: Grabbing values between quotation marks
Thanks

You can use negative look behind like this:
(?<!\\)"(.*?)(?<!\\)"
Check see it in action here on regex101
The first match group contains:
Hello \"World\"!

Related

Regex in Flutter to find double quotes enclosed words and escaped single quotes

I am using this Regex in my Flutter App to find words enclosed by single-quotes that end with a .tr:
r"'[^'\\]*(?:\\.[^'\\]*)*'\s*\.tr\b"
Now I need another expression that is almost the same but looks for words enclosed by dobule-quotes, ending with .tr and might contain escaped single-quotes.
I tried simply changing the single quotes to double quotes from the first expression, but Flutter is giving me errors... I need to escaped some characters but I can not make it work. Any idea?
An edge case it should match is:
"Hello, I\'m Chris".tr
You may use this regex for double quoted text that can have any escaped character followed by .tr and word boundary:
r""""[^"\\]*(?:\\.[^"\\]*)*"\s*\.tr\b"""
RegEx Demo
you need to use \ before every " in your RegExp's source, try this:
RegExp regExp = new RegExp(r'\"[^\"\\]*(?:\\.[^\"\\]*)*\"\s*\.tr\b');
print("${regExp.hasMatch('"Hello, I\'m Chris".tr')}"); // result = true

Extracting quotes between apostrophes in Python

I'm working on a regex to capture the text in quotes. It works, however the plain text which is the source file, has converted single smart quotes to apostrophes.
For the regex I have:
r("[\"|\'|\`].+[\"|\'|\`]")
The regex works fine but also grabs text between two apostrophes as well. Is is possible to adjust the regex so it doesn't do this?
"Come up and see me some time" # correct
'Yeah, I wonder if will pick this up to' #correct
`Mmmm. I wonder...` # correct
"Sorry about the mess!" #correct
We don't know who is human. Don't we? # Wrong.
The last one grabs
't know who is human. Don'
I would also recommend non-word boundaries (\B) like #Wiktor commented, but also use a backreference (\1) to match the same quote character as the starting quote character:
regex = r"\B([\"'`]).+?\1\B"
test it here https://regex101.com/r/TOLYVc/3

Regex matching, but not inside latex environment

I want to replace quotation marks in a latex document. It's written in German, which means that all quotation marks should be of the form "´text"' but some editors of the document have used these: "text", ´´text''.
The complication here is, that the document contains highlighted code using the lstlisting enviroment. In there the quotation marks should not be replaced.
I have a regex, that matches text inside the unwanted quotes, even if there are multiple words:
((``((\w+\s*)+)'')|("((\w+\s*)+)"))
I also have a regex, that matches a string ("asdf" in this case), only if it is not inside the lstlisting environment:
"asdf"(?=((?!\\end\{lstlisting\}).)*\\begin\{lstlisting\}?)
They work fine on their own, but when I combine them like this:
((``((\w+\s*)+)'')|("((\w+\s*)+)"))(?=((?!\\end\{lstlisting\}).)*\\begin\{lstlisting\}?)
some of the quoted strings, that should be matched are not and additionally the whole document is matched.
PS: I am currently using notepad++ for matching, because it allows . to match \n
[EDIT]: It works fine, as long as I limit the first part to single words:
((``((\w)+)'')|("((\w)+)"))(?=((?!\\end\{lstlisting\}).)*\\begin\{lstlisting\}?)
To match words with whitespaces, you can use
(``[\w\s]+''|"[\w\s]+")(?=(?:(?!\\end\{lstlisting\}).)*\\begin\{lstlisting\}?)
See regex demo
If you have spaces only between `` and '', or between "s, you will need to unroll the [\w\s]+ part as \w+(?:\s+\w+)*.

Using regex to replace unescaped quotes

I'm trying to use a regex search and replace to find and fix any unescaped quotation marks with escaped question marks. This is not in any particular language - just using regex to search and replace in Sublime Text 2.
I can find them just fine with this regex:
([a-zA-Z0-9!##$%^&*()_+=-\?><:;\/])\"
Trying to replace is giving me some headaches. I thought this would work:
$0\\\"
but it's adding an extra quote in (or leaving the previous one there somehow).
e.g.,
e"
becomes
e"\"
instead of just
e\"
What the hey? I can't seem to find a combination in the replacement that will work!
In the replacement $0 will be a reference to the entire match, including the quote. It looks like you should be using $1 instead which will be the first capturing group, so just the character immediately before the quote. So your replacement string would be "$1\\\"".

Regex to match word only in quotes

I'm trying to prepare a regex to match a word if it is in quotes.
i.e If the text is as follows, I want to match HelloWorld inside quotes but not the other one not in quotes. (should match 2nd instance of HelloWorld but not the 1st one)
HelloWorld " Showing HelloWorld"
Use Case:
I need to find text HelloWorld inside quotes but not other HelloWorld instances used as variable names or class names when I search in Eclipse IDE by regex.
You could use the following.
'HelloWorld " Showing HelloWorld"'.match(/"(.*?)"+/g); //["" Showing HelloWorld""]
As a quick-and-dirty possibility you could scan the string with this:
/"[^"]*?"/
This will match the first pair of quotes (and the contained text) in the string (note the use of *? for a "minimal" match). Do that globally in a loop to extract text inside quotes. Every time you match a new quoted piece, check it to see if it contains what you're looking for (possibly with another regex).
This will fail if there is an escaped quote in the string, although this could of course be dealt with.
if all patterns will be like
" Showing HelloWorld"
you can match using this
/".+\s(\w+)"/g
if your language supports [:alpha:]
you can match only letters
/".+\s([[:alpha:]]+)"/g
matching against \w+ can get both letters and numbers