Regex : Everything in group except white space - regex

I have this regex right here :
^(#include rem\(\s*(.*)),\s*(.*)\)
That matches this string :
#include rem( padding-top, $alert-padding );
I want to be able that the group with $alert-padding ignores the white space at the end. I tried doing :
^(#include rem\(\s*(.*)),\s*(/S)\)
replace the .* by /S but it doesn't match.
You can play around with the regex here :
https://regex101.com/r/9rouVU/1/

You may use \S+ to match 1 or more non-whitespace characters:
^(#include rem\(\s*(\S+))\s*,\s*(\S+)\s*\)
See the regex dem0
Details:
^ - start of string
(#include rem\(\s*(\S+)) - Group 1 capturing:
#include rem\( - a literal substring #include rem(
\s* - 0+ whitespaces
(\S+) - Group 2 capturing 1+ non-whitespace symbols
\s*,\s* - 0+ whitespaces, , and again 0+ whitespaces
(\S+) - 1+ non-whitespace symbols
\s* - 0+ whitespaces
\) - a literal ).

You can make the match in the second group lazy and then match for further optional whitespace:
^(#include rem\(\s*(.*)),\s*(.*?)\s*\)

Related

Regex separate digts and chars into groups

I try to seperate any string into 2 groups, digits and chars and eliminate all whitespace between this 2 groups. And after the first digit chars are allowed.
The (\D*)(\S+) works so far well for me except for the whitespace after the 1 group of chars.
Here is my regex demo.
You could exclude matching the whitespace chars as well using a negated character class [^\d\s]+ matching 1+ times any char except a whitespace char or a digit.
You can match optional whitespace chars using \s*
([^\d\s]+)\s*(\S+)
Explanation
( Capture group 1
[^\d\s]+ Match 1+ chars except a digit or whitespace char
) Close group
\s* Match 0+ non whitespace chars
(\S+) Capture group 2, match 1+ times a non whitespace char
Regex demo

REGEX NOTEPAD ++

I have a list in this format
FIRSTTEXT:SECONDTEXT:RANDOMTEXT::::::::RANDOMNUMBERS:NUMBER:
but all the text is not in this format. i want to save only FIRSTTEXT:SECONDTEXT,
firsttext and secondtext are in the same position on all document !
I have tried this one:
Find what: (.+):(.+)
Replace with: \1:\2
However, it doesn't work.
You may use
Find What: ^(?:([^:\s]+:[^:\s]+).*|.*\R*)
Replace With: $1
Details
^ - start of a line
(?: - start of a non-capturing group:
([^:\s]+:[^:\s]+) - Group 1 ($1 refers to this value):
[^:\s]+ - 1+ chars other than whitespace and :
: - a colon
[^:\s]+ - 1+ chars other than whitespace and :
.* - 0+ chars other than any line break char, as many as possible
| - or
.* - 0+ chars other than any line break char, as many as possible
\R* - 0+ line break sequences
) - end of the non-capturing group.
Demo and settings:

Matching the character before a linebreak, excluding whitespaces?

So I currently have a regex (https://regex101.com/r/zBE4Ju/1) that highlights the words before and after a linebreak. This is nice, but the issue is sometimes there are whitespaces after the word that appears BEFORE the line break. So they end up
You can see on my regex101 how the issue happens, and I have outlined the problem. I need to recognize the word before and after the line break, regardless of if there is a space after the word.
(\w*(?:[\n](?![\n])\w*)+)
You can see it in action here https://regex101.com/r/zBE4Ju/3
Expected: Line 1
Actual: Line 3
You can use $1 from:
/([^ ]+) *(\r|\n)/gm
https://regex101.com/r/o87VP7/5
If you want to highlight the last "word" in the sentence followed by possible spaces and a newline, you could repeat 0+ times a group matching 1+ non whitespace chars followed by 1+ spaces.
Then capture in a group matching non whitespace chars (\S+) and match possible spaces followed by a newline.
^ *(?:\S+ +)*(\S+) *\r?\n
Explanation
^ Start of string
* Match 0+ times a space
(?: Non capturing group
\S+ + Match 1+ non whitespace chars and 1+ spaces
-)* Close non capturing group and repeat 0+ times (to also match a single word at the beginning)
(\S+) Capture group 1, match 1+ times a non whitespace char
*\r?\n Match 0+ times a space followed by a newline
Regex demo

Regex - ignoring quotes

I'm using the regex
(?:^|;)\s*([^=]*[^=\s])\s*=\s*([^;]*[^;\s])
on the following string
"""A"" = .B; ""C"" = .D; ""E"" = .F"
The second capture group ([^;]*[^;\s]) matches the text .B, .D and .F", whilst the first capture group matches the text """A"", "C"" and ""E"".
How can I update this regex to match the text only, i.e., .B, .D and .F, and A, C and E?
I've tried add the quoted to the capture groups, e.g., ([^=\"]*[^=\s]), but this seems to have no affect.
You may match zero or more quotes before the key value and then restrict the [^=\s] character class to avoid matching " by adding it to the class and again match 0+ quotes right after:
(?:^|;)\s*"*([^=]*[^=\s"])"*\s*=\s*([^;]*[^;\s"])
^^ ^ ^^ ^
See the regex demo. Note that [^;]* will also match double quotes if any since it is a greedy pattern.
Details
(?:^|;) - start of string or ;
\s* - 0+ whitespaces
"* - 0+ double quotes
([^=]*[^=\s"]) - Group 1:
[^=]* - 0+ chars other than =
[^=\s"] - a char other than =, whitespace and "
"* - 0+ double quotes
\s*=\s* - a = enclosed with 0+ whitespaces
([^;]*[^;\s"]) - Group 2:
[^;]* - 0+ chars other than ;
[^;\s"] - a char other than ;, whitespace and ".

Regular Expression not grouping

Need help with this regex
ABC 130 zlis 02-03/12 N180 Grouping req
A B Csd 130 pain 02/12 I80 alias
(\w+\s{0,3})(\d+)
The regex does not seem to group as I need it to.
Desired Output, brackests are the groups im trying to detect.
(A B Csd) (130) (pain) (02/12) (I80) (alias)
Try this regex:
([a-z ]+?)\s+(\d+)\s+([a-z]+)\s+([\d-\/]+)\s+([\w ]+)
Click for Demo
Explanation:
([a-z ]+?) - match 1+ occurrences(as few as possible) of a letter or a space and capture it as Group1
\s+ - matches 1+ occurrences of a whitespace character
(\d+) - match 1+ occurrences of digits and capture as Group2
\s+ - matches 1+ occurrences of a whitespace character
([a-z]+) - match 1+ occurrences of a letter and Capture as Group 3
\s+ - matches 1+ occurrences of a whitespace character
([\d-\/]+) - match 1+ occurrences of a digit or - or / and capture it as Group4
\s+ - matches 1+ occurrences of a whitespace character
([\w ]+) - match 1+ occurrences of a word-character or a space and capture as Group5
Note that I have used the g, i, m flags for Global matches, Case-insensitive and Multiline respectively.