How do I get the substring " It's big \"problem "
using a regular expression?
s = ' function(){ return " It\'s big \"problem "; }';
How do I get the substring " It's big \"problem "
using a regular expression?
s = ' function(){ return " It\'s big \"problem "; }';
This one comes from nanorc.sample available in many linux distros. It is used for syntax highlighting of C style strings
One has to remember that regexps aren't a silver bullet for everything string-y. Some stuff are simpler to do with a cursor and linear, manual, seeking. A CFL would do the trick pretty trivially, but there aren't many CFL implementations (afaik).
I faced a similar problem trying to remove quoted strings that may interfere with parsing of some files.
I ended up with a two-step solution that beats any convoluted regex you can come up with:
Easier to read and probably more efficient.
Most of the solutions provided here use alternative repetition paths i.e. (A|B)*.
You may encounter stack overflows on large inputs since some pattern compiler implements this using recursion.
Java for instance: http://bugs.java.com/bugdatabase/view_bug.do?bug_id=6337993
Something like this:
"(?:[^"\\]*(?:\\.)?)*"
, or the one provided by Guy Bedford will reduce the amount of parsing steps avoiding most stack overflows.An option that has not been touched on before is:
This has the added bonus of being able to correctly match escaped open tags.
Lets say you had the following string;
String \"this "should" NOT match\" and "this \"should\" match"
Here,\"this "should" NOT match\"
should not be matched and"should"
should be. On top of thatthis \"should\" match
should be matched and\"should\"
should not.First an example.
Okay, now to explain the RegExp. This is the regexp can be easily broken into three pieces. As follows:
This is probably a lot clearer in image form: generated using Jex's Regulex
Image on github (JavaScript Regular Expression Visualizer.) Sorry, I don't have a high enough reputation to include images, so, it's just a link for now.
Here is a gist of an example function using this concept that's a little more advanced: https://gist.github.com/scagood/bd99371c072d49a4fee29d193252f5fc#file-matchquotes-js
should work with any quoted string