Short Answer:
A simple regex for this purpose would be:
/'[^']+'|[^\s]+/g
Sample code:
data = "This-is-first-token This-is-second-token 'This is third token'";
data.match(/'[^']+'|[^\s]+/g);
Result:
["This-is-first-token", "This-is-second-token", "'This is third token'"]
Explanation:

Debuggex Demo
I think this is as simple as you can make it in just a regex.
The g at the end makes it a global match, so you get all three matches. Without it, you get only the first string.
\s matches all whitespace (basically, and tabs, in this instance). So, it would work even if there was a tab between This-is-first-token and This-is-second-token.
To match content in braces, use this:
data.match(/\{[^\}]+\}|[^\s]+/g);

Debuggex Demo
Braces or single quotes:
data.match(/\{[^\}]+\}|'[^']+'|[^\s]+/g);

Debuggex Demo
Answer from elixenide on Stack OverflowShort Answer:
A simple regex for this purpose would be:
/'[^']+'|[^\s]+/g
Sample code:
data = "This-is-first-token This-is-second-token 'This is third token'";
data.match(/'[^']+'|[^\s]+/g);
Result:
["This-is-first-token", "This-is-second-token", "'This is third token'"]
Explanation:

Debuggex Demo
I think this is as simple as you can make it in just a regex.
The g at the end makes it a global match, so you get all three matches. Without it, you get only the first string.
\s matches all whitespace (basically, and tabs, in this instance). So, it would work even if there was a tab between This-is-first-token and This-is-second-token.
To match content in braces, use this:
data.match(/\{[^\}]+\}|[^\s]+/g);

Debuggex Demo
Braces or single quotes:
data.match(/\{[^\}]+\}|'[^']+'|[^\s]+/g);

Debuggex Demo
You can use this split:
var string = "This-is-first-token This-is-second-token 'This is third token'";
var arr = string.split(/(?=(?:(?:[^']*'){2})*[^']*$)\s+/);
//=> ["This-is-first-token", "This-is-second-token", "'This is third token'"]
This assumes quotes are all balanced.
I keep on having this problem.
Lets say I want to take {p = v} and match p and v where they can be just about anything. I also want to make sure it isn't escaped
As of now, I have the following: (in python):
r"(?<!\\)\{(.*?)\=(.*?)\}"That works 99% of the time, but I came across an edge case I hadn't considered. I get a false-positive match on the following two:
{ "th=st" } # Shouldn't match at all
{ 'a=b' = 20 } # Should match " 'a=b' " and "20" but messes it up
In this case, I do not want it to match since the = is inside a quote but I do not want to assume = is always inside a quote.
Is this possible?
Thanks!
Update: I should explain where I am trying to run this. It is on the solarwinds network monitoring product which has a tool for comparing configuration files. This tool has an option to ignore any config lines that are matched by a regex expression, so I suppose the correct title to this post is: "Match all text between two strings".
Adding to the mix I believe it uses the GNU Diffutils package, so it uses uses grep-style regular expressions.(https://www.gnu.org/software/grep/manual/grep.html#Regular-Expressions).
I have this snippet:
interface GigabitEthernet1/0/47 description ** switchport access vlan 1 switchport mode access switchport port-security violation restrict switchport port-security aging time 2 switchport port-security aging type inactivity switchport port-security no logging event link-status storm-control broadcast level 4.00 storm-control multicast level 10.00 storm-control action shutdown storm-control action trap no cdp enable spanning-tree portfast spanning-tree bpduguard enable spanning-tree guard root ip dhcp snooping limit rate 10 ! interface GigabitEthernet1/0/48
And I would like to ignore everything between
interface GigabitEthernet1/0/47
&
interface GigabitEthernet1/0/48
I can match the strings exactly using something like
(interface GigabitEthernet1\/0\/47)+
But I have no idea how to ignore everything in between these strings!
Any help would be greatly appreciated.
You can try this:
[^\w +-]
REGEX EXPLANATION
[^\w +-]
Match a single character NOT present in the list below «[^\w +-]»
A word character (letters, digits, and underscores) «\w»
The character “ ” « »
The character “+” «+»
The character “-” «-»
You can use the following. Simply add these characters inside of your negated character class.
Within a character class [], you can place a hyphen (-) as the first or last character. If you place the hyphen anywhere else you need to escape it (\-) in order to be matched.
Pattern p = Pattern.compile("(?i)[^a-z0-9 +-]");
Regular expression:
(?i) # set flags for this block (case-insensitive)
[^a-z0-9+-] # any character except: 'a' to 'z', '0' to '9', ' ', '+', '-'
That's why quantifiers are for.
^(?:[^|]*\|){10}([^|]*)
If you're using a regex in the context of another programming language (Python, C#, etc.), that language likely has some type of string splitting function. In my experience, it's usually easier to split on the delimiter and get a list/array of values instead of using regexes to split.
You might use
(?<!\[\[|\*)\*\*(?!\*)(.+?)(?<!\*)\*\*(?!\*|]])
The pattern matches
(?<!\[\[|\*)Negative lookbehind, assert[[or*to the left\*\*(?!\*)Match**and negative lokoahead to assert not*to the right(.+?)Capture group 1, match 1+ chars as least as possible(?<!\*)\*\*Negative lookbehind, assert not*to the left and match**(?!\*|]])Negative lookahead, assert not*or]]to the right
Regex demo | Php demo
Another option might be matching all that you don't want and then making use of SKIP FAIL
(?:\[\[.*?]]|\*{3,}.*?\*+|\*+.*?\*{3,})(*SKIP)(*F)|\*\*(.+?)\*\*
Regex demo | Php demo
Without seeing how much variability there is in your input string, I'd say you can simply match and discard the substrings that are double square brace and double asterisk wrapped and then match&capture the non brace wrapped substrings that are only asterisk wrapped.
There are fringe cases (like nested square braced substrings) where this will fail, but I don't know if they are reasonably likely to occur.
Code: (Demo)
$text = <<<TEXT
Text **bold** [[**not
bold**]]
TEXT;
echo preg_replace(
'~\[{2}\*{2}.*?\*{2}]{2}(*SKIP)(*FAIL)|\*{2}(.*?)\*{2}~s',
'<b>$1</b>',
$text
);
Output:
Text <b>bold</b> [[**not
bold**]]