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 Overflow
🌐
Reddit
reddit.com › r/regex › is it possible to ignore characters in between quotes?
r/regex on Reddit: Is it possible to ignore characters in between quotes?
November 16, 2017 -

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!

🌐
QBasic on Your Computer
chortle.ccsu.edu › finiteautomata › Section07 › sect07_12.html
Basic Regular Expressions: Exclusions
except a list of excluded characters, put the excluded charaters between [^ and ]. The caret ^ must immediately follow the [ or else it stands for just itself.
🌐
RegexOne
regexone.com › lesson › excluding_characters
RegexOne - Learn Regular Expressions - Lesson 4: Excluding specific characters
In some cases, we might know that there are specific characters that we don't want to match too, for example, we might only want to match phone numbers that are not from the area code 650.
🌐
Reddit
reddit.com › r/regex › ignore all between two strings
r/regex on Reddit: Ignore all between two strings
December 5, 2019 -

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.

Top answer
1 of 3
4
You can't really "remove" the middle part, but you can select only the interface lines... knowing what you're using for this would be helpful. Regex: (?ms)(interface GigabitEthernet1\/0\/47).*(interface GigabitEthernet1\/0\/48) Regex101 link: https://regex101.com/r/tg2wTD/1 Basic example in PowerShell: $string = @' 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 '@ $regex = "(?ms)(interface GigabitEthernet1\/0\/47).*(interface GigabitEthernet1\/0\/48)" if ($string -match $regex) { $Matches[1], $Matches[2] }
2 of 3
1
howdy ing80nFU4r225KrEgEBP, you likely otta add your environment to your OP so folks will know what your limits are. [grin] from what i understand of your post, all you want is to capture the two lines that start with interface. in powershell, i would do the following ... $InStuff -match 'interface' $InStuff -match '^interface' presuming your data was loaded into $InStuffas collection of lines via Get-Content, the 1st line would give you each line that contains the target word. the 2nd line would give you only lines that START with the target word. take care, lee
🌐
Regex Tester
regextester.com › 97777
exclude text between two curly brackets - Regex Tester/Debugger
Url checker with or without http:// or https:// Match string not containing string Check if a string only contains numbers Only letters and numbers Match elements of a url date format (yyyy-mm-dd) Url Validation Regex | Regular Expression - Taha Match an email address Validate an ip address nginx test Extract String Between Two STRINGS match whole word Match anything enclosed by square brackets.
🌐
SitePoint
sitepoint.com › javascript
Ignore a Part of a String Using REGEX - JavaScript
April 30, 2020 - I’m trying to get only the domain name and the top level domain from URLs and ignore the rest using REGEX and I’m not sure if it’s correct although it does match what I’m looking for. In the URLs below I only want the domain name followed by dot net, dot com, dot biz, dot org, etc., and what ever comes after the top level domain: http://example.com/maps https://www.example.biz http://www.example.org/test http://www.example.net I have used the REGEX pattern below to match what I need and it ...
Find elsewhere
🌐
Stack Overflow
stackoverflow.com › questions › 72778811 › regex-ignore-characters-in-quotes
Regex Ignore Characters in quotes - Stack Overflow
You can get a single match with separate parts. You can get the parts using 2 capture groups (raw_pmc_df\[').*?(']) regex101.com/r/226v9l/1 ... It seems you want to remove these raw and [' with '], and keep the contents that are in between, correct? Then use raw_pmc_df\[\'(.*?)'] and replace with $1 (or \1, depending on your programming environment).
🌐
Keyboard Maestro
forum.keyboardmaestro.com › questions & suggestions
How to ignore portions of a Variable using RegEx? - Questions & Suggestions - Keyboard Maestro Discourse
April 23, 2018 - How can I omit a character string in the middle of a variable using RegEx? Specifically, I need to delete all characters between "Ring" and "[Metal Type]" in the following examples: Ring - 8mm - Flat - 9 - White Gold - FingerPrint Ring - 7mm - Flat - 8 - Stainless Steel - FingerPrint Ring - 6mm - Half-Round - 8 - Rose Gold - FingerPrint I'm trying to match a "Product" variable to a generic "Template" file using a "Switch" action and don't care about the width, profile & size info contained...
🌐
NTU Singapore
www3.ntu.edu.sg › home › ehchua › programming › howto › Regexe.html
Regular Expression (Regex) Tutorial
For example, the regex [02468] matches a single digit 0, 2, 4, 6, or 8; the regex [^02468] matches any single character other than 0, 2, 4, 6, or 8. Instead of listing all characters, you could use a range expression inside the bracket. A range expression consists of two characters separated by a hyphen (-). It matches any single character that sorts between the two characters, inclusive.
🌐
UiPath Community
forum.uipath.com › help
Regex ignore after help - Help - UiPath Community Forum
October 24, 2018 - Hi. I am working with a long string and extracting certain pieces of information from it, where it is in a static place within the string. I now have an issue where I have some text that can change length. I need to extract just numbers and the decimal place which I am doing by retrieving the first 25 characters of this section of the string by using the Regex statement of System.Text.RegularExpressions.Regex.Replace(strVolume,“[^/./0-9]”,“”) My issue is, there can be an additional number wi...
🌐
Notepad++ Community
community.notepad-plus-plus.org › topic › 25626 › how-to-ignore-certain-characters-in-searches
How to ignore certain characters in searches | Notepad++ Community
March 28, 2024 - Searching for best I could, it would fail, but searching in regex mode for best\s+I\s+could, it would find it (\s+ means “one or more whitespace characters”, where “whitespace characters” include space, tab, newline, and some other Unicode characters as well)
🌐
UiPath Community
forum.uipath.com › help › studio
Ignore all characters other than number digits and negative sign - Studio - UiPath Community Forum
August 9, 2023 - Currently I’m using this Regex code I found from other forums: However, this also removes the negative sign, which I’ll need to be able to differentiate between positive value and negative value. How can I be able to…
🌐
UiPath Community
forum.uipath.com › help › activities
Regex to ignore multiple text to match two strings - Activities - UiPath Community Forum
October 11, 2022 - Hi, I need help on regex to check if two strings is match and ignoring multiple strings on both, white spaces and special characters. Example: String1 = “ABC-Corp.” String2 = “ABC Corporation” Output: True S…