using "or" in pattern matching regular expression - regex

I am using a regular expression to find the matched pattern. But somehow I am not able to find all Occrences.
My input file from where I need to match the pattern(please note that this is an example file with only 3 occurrence, in real - it have multiple Occrences) :
aaa-233- hi, how are you?
aaa-234- 6(-8989)
aaa-235- 123
end
So, I want my output to be
hi, how are you?
6(-8988)
123
My regex is
aaa\\-[A-Za-z0-9,->#]\\-(.+?)(aaa)
Pseudo code
Output= matcher.group(2);
How can I make the logic to start read from aaa and end either it encounters aaa or end.

Use
(?sm)^aaa-[^-]+-.*?(?=\naaa|\nend|\z)
See proof
Explanation
EXPLANATION
--------------------------------------------------------------------------------
(?ms) set flags for this block (with ^ and $
matching start and end of line) (with .
matching \n) (case-sensitive) (matching
whitespace and # normally)
--------------------------------------------------------------------------------
^ the beginning of a "line"
--------------------------------------------------------------------------------
aaa- 'aaa-'
--------------------------------------------------------------------------------
[^-]+ any character except: '-' (1 or more times
(matching the most amount possible))
--------------------------------------------------------------------------------
- '-'
--------------------------------------------------------------------------------
.*? any character (0 or more times (matching
the least amount possible))
--------------------------------------------------------------------------------
(?= look ahead to see if there is:
--------------------------------------------------------------------------------
\n '\n' (newline)
--------------------------------------------------------------------------------
aaa 'aaa'
--------------------------------------------------------------------------------
| OR
--------------------------------------------------------------------------------
\n '\n' (newline)
--------------------------------------------------------------------------------
end 'end'
--------------------------------------------------------------------------------
| OR
--------------------------------------------------------------------------------
\z the end of the string
--------------------------------------------------------------------------------
) end of look-ahead

Related

Regex function to match a specific sequence of characters

I'm looking for a regex that would be True only in case that:
it starts with ${ (1 time, $ can only be there one time)
after that any characters or nothing until } is found
This would match:
${
${test
test${test
${$fdsf$
${test}
This would not match:
$${
$${test
${test}test
Hopefully it's clear :)
Is that possible ?
Does that work for you?
(?<!\$)\$\{[^}]*}?$
https://regex101.com/r/g0vhJo/2
Also use
([^$\n]|^)\$\{[^}\n]*}?$
See regex proof.
EXPLANATION
--------------------------------------------------------------------------------
( group and capture to \1:
--------------------------------------------------------------------------------
[^$\n] any character except: '$', '\n'
(newline)
--------------------------------------------------------------------------------
| OR
--------------------------------------------------------------------------------
^ the beginning of the string
--------------------------------------------------------------------------------
) end of \1
--------------------------------------------------------------------------------
\$ '$'
--------------------------------------------------------------------------------
\{ '{'
--------------------------------------------------------------------------------
[^}\n]* any character except: '}', '\n' (newline)
(0 or more times (matching the most amount
possible))
--------------------------------------------------------------------------------
}? '}' (optional (matching the most amount
possible))
--------------------------------------------------------------------------------
$ before an optional \n, and the end of the
string

Inverse Regular Expressions Match in Webpack SVGO-Loader

I have a regular expression looking for .svg files under the icons-wc/icons directory for a Webpack svgo-loader.
/icons-wc\\icons\\.*\.svg$/
I'd now like to find all .svg files outside the icons-wc/icons directory but I'm not sure how to approach it. I've tried something like this but that doesn't seem to work. It seems to be too over eager to select
/(?<!icons-wc\\icons)\\.*\.svg$/
Use
/^(?!.*(?:^|[\\\/])icons-wc[\\\/]icons[\\\/]).*\.svg$/
See regex proof.
EXPLANATION
--------------------------------------------------------------------------------
^ the beginning of the string
--------------------------------------------------------------------------------
(?! look ahead to see if there is not:
--------------------------------------------------------------------------------
.* any character except \n (0 or more times
(matching the most amount possible))
--------------------------------------------------------------------------------
(?: group, but do not capture:
--------------------------------------------------------------------------------
^ the beginning of the string
--------------------------------------------------------------------------------
| OR
--------------------------------------------------------------------------------
[\\\/] any character of: '\\', '\/'
--------------------------------------------------------------------------------
) end of grouping
--------------------------------------------------------------------------------
icons-wc 'icons-wc'
--------------------------------------------------------------------------------
[\\\/] any character of: '\\', '\/'
--------------------------------------------------------------------------------
icons 'icons'
--------------------------------------------------------------------------------
[\\\/] any character of: '\\', '\/'
--------------------------------------------------------------------------------
) end of look-ahead
--------------------------------------------------------------------------------
.* any character except \n (0 or more times
(matching the most amount possible))
--------------------------------------------------------------------------------
\. '.'
--------------------------------------------------------------------------------
svg 'svg'
--------------------------------------------------------------------------------
$ before an optional \n, and the end of the
string

regexp: multiline, non-greedy match until optional string

Using Go's regexp, I'm trying to extract a predefined set of ordered key-value (multiline) pairs whose last element may be optional from a raw text, e.g.,
Key1:
SomeValue1
MoreValue1
Key2:
SomeValue2
MoreValue2
OptionalKey3:
SomeValue3
MoreValue3
(here, I want to extract all the values as named groups)
If I use the default greedy pattern (?s:Key1:\n(?P<Key1>.*)Key2:\n(?P<Key2>.*)(?:OptionalKey3:\n(?P<OptionalKey3>.*))?), it never sees OptionalKey3 and matches the rest of the text as Key2.
If I use the non-greedy pattern (?s:Key1:\n(?P<Key1>.*)Key2:\n(?P<Key2>.*?)(?:OptionalKey3:\n(?P<OptionalKey3>.*))?), it doesn't even see SomeValue2 and stops immediately: https://regex101.com/r/QE2g3o/1
Is there a way to optionally match OptionalKey3 while also able to capture all the other ones?
Use
(?s)\AKey1:\n(?P<Key1>.*)Key2:\n(?P<Key2>.*?)(?:OptionalKey3:\n(?P<OptionalKey3>.*))?\z
See regex proof.
EXPLANATION
--------------------------------------------------------------------------------
(?s) set flags for this block (with . matching
\n) (case-sensitive) (with ^ and $
matching normally) (matching whitespace
and # normally)
--------------------------------------------------------------------------------
\A the beginning of the string
--------------------------------------------------------------------------------
Key1: 'Key1:'
--------------------------------------------------------------------------------
\n '\n' (newline)
--------------------------------------------------------------------------------
(?P<Key1> group and capture to "Key1":
--------------------------------------------------------------------------------
.* any character (0 or more times (matching
the most amount possible))
--------------------------------------------------------------------------------
) end of "Key1"
--------------------------------------------------------------------------------
Key2: 'Key2:'
--------------------------------------------------------------------------------
\n '\n' (newline)
--------------------------------------------------------------------------------
(?P<Key2> group and capture to "Key2":
--------------------------------------------------------------------------------
.*? any character (0 or more times (matching
the least amount possible))
--------------------------------------------------------------------------------
) end of "Key2"
--------------------------------------------------------------------------------
(?: group, but do not capture (optional
(matching the most amount possible)):
--------------------------------------------------------------------------------
OptionalKey3: 'OptionalKey3:'
--------------------------------------------------------------------------------
\n '\n' (newline)
--------------------------------------------------------------------------------
(?P<OptionalKey3> group and capture to "OptionalKey3":
--------------------------------------------------------------------------------
.* any character (0 or more times
(matching the most amount possible))
--------------------------------------------------------------------------------
) end of "OptionalKey3"
--------------------------------------------------------------------------------
)? end of grouping
--------------------------------------------------------------------------------
\z the end of the string

need regex command to extract full filename which has 2 dot(.)

i am having issue in extracting full filename which has 2 dot(.).
although below command is working but i need a alternate solution without asterix in regex. can anyone help me out in alternate regex command to extract full filename without asterix?
(ABC_A.*\.)+.*
Here are filenames I am trying to match:
ABC_A_CommunityRollover_Autocreate_Community.12345678-1.out
ABC_A_CommunityRollover_Autocreate_Community.88345678-1.out
ABC_A_CommunityRollover_Autocreate_Community.99945678-1.out
TL;DR:
^ABC_A(?:[^.]+\.){2}[^.]+$
Live Demo.
EXPLANATION
--------------------------------------------------------------------------------
^ the beginning of the string
--------------------------------------------------------------------------------
ABC_A 'ABC_A'
--------------------------------------------------------------------------------
(?: group, but do not capture (2 times):
--------------------------------------------------------------------------------
[^.]+ any character except: '.' (1 or more
times (matching the most amount
possible))
--------------------------------------------------------------------------------
\. '.'
--------------------------------------------------------------------------------
){2} end of grouping
--------------------------------------------------------------------------------
[^.]+ any character except: '.' (1 or more times
(matching the most amount possible))
--------------------------------------------------------------------------------
$ before an optional \n, and the end of the
string

REGEX-Match characters present in a string

What is the REGEX to accept a string like this
Starts with EDO
has many characters(words,numbers,hypehns) in between
does not contain 24 or |(pipe)
Example:
Should match
edo-<<characters>>-<<characeters>>-<<numbers>>
BUT NOT
edo-<<characters>>-<<characeters>>-<<numbers>> | <<characeters>>- <<characeters>>- <<numbers>>
The string does not have a constant length
The negative look ahead will help you to decide if the string doesnt contain 24 or |
The regex can be written as
/^edo(?!.*(24|\|))[-a-zA-Z0-9]+$/i
Regex Demo
How it matches
^ Anchors the regex at the start of the string
edo The anchor ensures that the string starts with edo
(?!.*(24|\|)) look ahead assertion. It checks if the string doesnt contain 24 or |. If it doesnt contain, then proceeds with the remaining pattern. If it contains, discards the match
[-a-zA-Z0-9]+ Matches numbers alphabets or -
$ anchors the regex at the end of the string.
^EDO(?!.*(?:(?<!\d)24(?!\d)|\|))[a-zA-Z0-9 -]+$
Try this.This should work.Use flag gmi.
See demo.
https://regex101.com/r/fA6wE2/37
NODE EXPLANATION
--------------------------------------------------------------------------------
^ the beginning of the string
--------------------------------------------------------------------------------
EDO 'EDO'
--------------------------------------------------------------------------------
(?! look ahead to see if there is not:
--------------------------------------------------------------------------------
.* any character except \n (0 or more times
(matching the most amount possible))
--------------------------------------------------------------------------------
(?: group, but do not capture:
--------------------------------------------------------------------------------
(?<! look behind to see if there is not:
--------------------------------------------------------------------------------
\d digits (0-9)
--------------------------------------------------------------------------------
) end of look-behind
--------------------------------------------------------------------------------
24 '24'
--------------------------------------------------------------------------------
(?! look ahead to see if there is not:
--------------------------------------------------------------------------------
\d digits (0-9)
--------------------------------------------------------------------------------
) end of look-ahead
| OR
--------------------------------------------------------------------------------
\| '|'
--------------------------------------------------------------------------------
) end of grouping
--------------------------------------------------------------------------------
) end of look-ahead
--------------------------------------------------------------------------------
[a-zA-Z0-9-]+ any character of: 'a' to 'z', 'A' to 'Z',
'0' to '9', '-' (1 or more times (matching
the most amount possible))
--------------------------------------------------------------------------------
$ before an optional \n, and the end of the
string