How to use lookahead to exclude special characters in a regular expression
The (.*\d+x\d+)*^(?![\+]+\d\+\d)
pattern matches and captures into Group 1 an optional sequence of any zero or more chars other than line break chars, as many as possible, then 1+ digits, x
, 1+ digits, and then requires the start of string position (this makes the pattern fail all the time!) and makes sure there is no one or more +
chars, a digit, a +
and a digit immediately to the right of the current location. This looks like a very corrupt pattern that never matches any string.
Your logic can be implemented as
xandr | grep -oP '\b\d+x\d+(?!(?:\+\d+\+)?\d)'
See the online demo and the regex demo.
Details:
-oP
- output matches only and enable PCRE regex engine\b\d+x\d+(?!(?:\+\d+\+)?\d)
:\b
- word boundary\d+
- one or more digitsx
- anx
\d+
- one or more digits(?!(?:\+\d+\+)?\d)
- a negative lookahead that fails the match if, immediately to the right of the current location, there is(?:\+\d+\+)?
- an optional sequence of+
, one or more digits and+
\d
- a digit.
Regex boundary to also exclude special characters
You may use lookahead and lookbehind on both sides to fail the match if there is a non-whitespace character on either side:
(?<!\S)word-to-match(?!\S)
RegEx Demo
(?<!\S)
: Fail if previous character is a non-whitespace(?!\S)
: Fail if next character is a non-whitespace
Related Topics
Selecting from Div Class Dropdown - Selenium
How to Open Excel File Using JavaScript Code Without Using Activex Control
How to Test for Blank Line With Java Scanner
How to Return a Zip File to the Browser Via the Response Outputstream
How to Pass the Same Object Across Multiple Classes Java
Eclipse: the Resource Is Not on the Build Path of a Java Project
How to Disable Error Highlighting in VS Code
How to Solve the "Failed to Lazily Initialize a Collection of Role" Hibernate Exception
Eclipse Problems View Not Showing Errors Anymore
How to Convert a One-Dimensional Array to Two-Dimensional Array in Java
Jpa: Update Only Specific Fields
Java.Lang.Noclassdeffounderror: Org/Json/Simple/Parser/Parseexception With Eclipse and Spring
How to Mock Classes With Constructor Injection
How to Return Different Types of Responseentity in Spring MVC or Spring-Boot
The Request Was Rejected Because No Multipart Boundary Was Found in Springboot
Cannot Construct Instance of 'Java.Time.Localdate' - Spring Boot, Elasticseach, Jackson