How to Grep Search two occurrences of a character in a lookbetween - regex

I seem to have to perpetually relearn Regex & Grep syntax every time I need something advanced. This time, even with BBEDIT's pattern playground, I can't work this one out.
I need to do a multi-line search for the occurrence of two literal asterisks anywhere in the text between a pair of tags in a plist/XML file.
I can successfully construct a lookbetween so:
(?s)(?<=<array>).*?(?=</array>)
I try to limit that to only match occurrences in which two asterisks appear between tags:
(?s)(?<=<array>).*?[*]{2}.*?(?=</array>)
(?s)(?<=<array>).+[*]{2}.+(?=</array>)
(?s)(?<=<array>).+?[*]{2}.+?(?=</array>)
But they find nought. And when I remove the {2} I realize I'm not even constructing it right to find occurrences of one asterisk. I tried escaping the character /* and [/*] but to no avail.
How can i match any occurrence of blah blah * blah blah * blah blah ?

[*]{2} means the two asterisks must be consecutive.
(.*[*]){2} is what you're looking for - it contains two asterisks, with anything in between them.
But we also need to make sure the regex is only testing one tag closure at the same time, so instead of .*, we need to use ((?!<\/array>).)* to make sure it won't consume the end tag </array> while matching .*
The regex can be written as:
(?s)(?<=<array>)(?:((?!<\/array>).)*?[*]){2}(?1)*
See the test result here

Use
(?s)(?<=<array>)(?:(?:(?!<\/?array>)[^*])*[*]){2}.*?(?=</array>)
See proof.
Explanation
NODE
EXPLANATION
(?s)
set flags for this block (with . matching \n) (case-sensitive) (with ^ and $ matching normally) (matching whitespace and # normally)
(?<=
look behind to see if there is:
  <array>
'<array>'
)
end of look-behind
(?:
group, but do not capture (2 times):
(?:
group, but do not capture (0 or more times (matching the most amount possible)):
(?!
look ahead to see if there is not:
</?array>
</array> or <array>
)
end of look-ahead
[^*]
any character except: '*'
)*
end of grouping
[*]
any character of: '*'
){2}
end of grouping
.*?
any character (0 or more times (matching the least amount possible))
(?=
look ahead to see if there is:
</array>
'</array>'
)
end of look-ahead

Related

Extract the last path-segments of a URI or path using RegEx

I am trying to extract the last section of the following string :
"/subscriptions/5522233222-d762-666e-555a-e6666666666/resourcegroups/rg-sql-Belguim-01/providers/Microsoft.Compute/snapshots/vm-sql-image-v3.3-pre-sysprep-Oct-2021-BG"
I want to capture:
"snapshots/vm-sql-image-v3.3-pre-sysprep-Oct-2021-BG"
I tried below with no luck:
(\w*?\/\w*?)$
How to pull this off using regex?
Use
[^\/]+\/[^\/]+$
See regex proof.
EXPLANATION
--------------------------------------------------------------------------------
[^\/]+ any character except: '\/' (1 or more
times (matching the most amount possible))
--------------------------------------------------------------------------------
\/ '/'
--------------------------------------------------------------------------------
[^\/]+ any character except: '\/' (1 or more
times (matching the most amount possible))
--------------------------------------------------------------------------------
$ before an optional \n, and the end of the
string
Your issues
(\w*?/\w*?)$ is for simple or empty last 2 segments (tested), e.g.
matched hello/world/subscriptions123/snap_shots capturing subscriptions123/snap_shots
matched /1/2// capturing the last 2 empty segments
OK was:
capture-group
/ to match the last path-separator before end ($)
\w*? intended to match the path-segment of any length
What to improve:
*? is a bit too unrestricted, choose quantifier as + for at least one (instead * for any or ? for zero or one)
\w is for word-meta-character, does not match hyphens or dots (OK for snapshot, not for given last segment)
Quick-fixed
(\w+/[\w\.-]+)$ (tested)
added dot \. and hyphen - to character-set containing \w
Simple but solid
(snapshots/[^\/]+)$ (tested)
fore-last path-segment assumed as fix constant snapshots
[^\/] any character except (^) slash in last segment
Note: the slash doesn't need to be escaped \/ like Ryszard answered

RegEx expression to keep first appearance of word grouping

I have the following RegEx (\[[^]]+])(?=.*\1)
which identifies the first set of appearances of a duplicate word group inside a string (each word group is enclosed between [ ] brackets). However, I am trying to come up with a RegEx that identifies the last set of appearances of a duplicate word group. Reason being, I need to remove duplicate word groups while retaining the order in which each group appears in the overall string.
Using the following string as an example whereby only [John Smith] and [Jane Doe] are duplicate word groups:
[John Smith][John Smith][Mr. Smith][Jane Doe][Mrs. Doe][John Smith][Jane Doe][Doe][John][Smith John][John Smith Sr]
After using my RegEx in a RegEx Replace formula, I get the below:
[Mr. Smith][Mrs. Doe][John Smith][Jane Doe][Doe][John][Smith John][John Smith Sr]"
However, I need my RegEx Replace formula to give me:
[John Smith][Mr. Smith][Jane Doe][Mrs. Doe][Doe][John][Smith John][John Smith Sr]
I have tried many ways to achieve the latter with no luck. Thanks in advance.
Considering infinite-width lookbehinds:
(\[[^\][]+])(?<=\1.*\1)
See regex proof.
EXPLANATION
--------------------------------------------------------------------------------
( group and capture to \1:
--------------------------------------------------------------------------------
\[ '['
--------------------------------------------------------------------------------
[^\][]+ any character except: '\]', '[' (1 or
more times (matching the most amount
possible))
--------------------------------------------------------------------------------
] ']'
--------------------------------------------------------------------------------
) end of \1
--------------------------------------------------------------------------------
(?<= look behind to see if there is:
--------------------------------------------------------------------------------
\1 what was matched by capture \1
--------------------------------------------------------------------------------
.* any character except \n (0 or more times
(matching the most amount possible))
--------------------------------------------------------------------------------
\1 what was matched by capture \1
--------------------------------------------------------------------------------
) end of look-behind
Many regex engines do not support variable-length lookbehinds, but most support variable-length lookaheads. When working with an engine that supports variable-length lookaheads, but not variable-length lookbehinds, one approach that often works is to reverse the string, modify the (reversed) string with a regex and then reverse the resulting string. That approach could be used here.
Suppose, for example, the string were
"[John Smith][John Smith][Mr. Smith][Jane Doe][John Smith][Jane Doe][Doe]"
Reversing the string produces
"]eoD[]eoD enaJ[]htimS nhoJ[]eoD enaJ[]htimS .rM[]htimS nhoJ[]htimS nhoJ["
We now convert matches of the following expression to empty strings:
(\][^[]*\[)(?=.*\1)
which produces
"]eoD[]eoD enaJ[]htimS .rM[]htimS nhoJ["
Demo
Lastly, we reverse that string to obtain
"[John Smith][Mr. Smith][Jane Doe][Doe]"
The regular expression can be written in free-spacing mode to make it self-documenting:
( # begin capture group 1
\] # match ']'
[^[]* # match one or more (as many as possible) chars other than '['
\[ # match '['
) # end capture group 1
(?= # begin a positive lookahead
.* # match one or more (as many as possible) chars
\1 # match the content of capture group 1
) # end the positive lookahead
At first glance this may seem a kludge, but since reversing a string is so easy in any language it does provide a useful work-around in some cases. Mind you, doing what you want to do here in code is pretty easy in most languages. In Ruby, for example, you could write (str being a variable holding the string)
str.scan(/\[[^\]]*\]/).uniq.join

Get first match in closing part of regex

I must take string with regex who got string "[%" "%]" and any text or "" inside this. As example:
Input: dsafsdfadsaffsdadsaffadsaf[%sadsad[%]%%]fdfsadfsad%]fsasdf
Output: [%sadsad[%]
I already wrote expression - \[%(.\n*)*%\], but it takes last of %].
Output: [%sadsad[%]%%]fdfsadfsad%]
Did anyone know how get first of closing match?
Put . and \n inside a capturing or non-capturing group delimited by a logical OR | operator, and make it as non-greedy.
\[%(.|\n)*?%\]
OR
You could do like the below.
\[%[\S\s]*?%\]
[\S\s]*? Matches any space or non-space character non-greedily.
\[%[^\]]*%\]
You can try this to get string upto first closng %].See demo.
https://regex101.com/r/gX5qF3/5
NODE EXPLANATION
--------------------------------------------------------------------------------
\[% '['%
--------------------------------------------------------------------------------
[^\]]* any character except: '\]' (0 or more
times (matching the most amount possible))
--------------------------------------------------------------------------------
%\] %']'

cryptic perl expression

I find the following statement in a perl (actually PDL) program:
/\/([\w]+)$/i;
Can someone decode this for me, an apprentice in perl programming?
Sure, I'll explain it from the inside out:
\w - matches a single character that can be used in a word (alphanumeric, plus '_')
[...] - matches a single character from within the brackets
[\w] - matches a single character that can be used in a word (kinda redundant here)
+ - matches the previous character, repeating as many times as possible, but must appear at least once.
[\w]+ - matches a group of word characters, many times over. This will find a word.
(...) - grouping. remember this set of characters for later.
([\w]+) - match a word, and remember it for later
$ - end-of-line. match something at the end of a line
([\w]+)$ - match the last word on a line, and remember it for later
\/ - a single slash character '/'. it must be escaped by backslash, because slash is special.
\/([\w]+)$ - match the last word on a line, after a slash '/', and remember the word for later. This is probably grabbing the directory/file name from a path.
/.../ - match syntax
/.../i - i means case-insensitive.
All together now:
/\/([\w]+)$/i; - match the last word on a line and remember it for later; the word must come after a slash. Basically, grab the filename from an absolute path. The case insensitive part is irrelevant, \w will already match both cases.
More details about Perl regex here: http://www.troubleshooters.com/codecorn/littperl/perlreg.htm
And as JRFerguson pointed out, YAPE::Regex::Explain is useful for tokenizing regex, and explaining the pieces.
You will find the Yape::Regex::Explain module worth installing.
#!/usr/bin/env perl
use YAPE::Regex::Explain;
#...may need to single quote $ARGV[0] for the shell...
print YAPE::Regex::Explain->new( $ARGV[0] )->explain;
Assuming this script is named 'rexplain' do:
$ ./rexplain '/\/([\w]+)$/i'
...to obtain:
The regular expression:
(?-imsx:/\/([\w]+)$/i)
matches as follows:
NODE EXPLANATION
----------------------------------------------------------------------
(?-imsx: group, but do not capture (case-sensitive)
(with ^ and $ matching normally) (with . not
matching \n) (matching whitespace and #
normally):
----------------------------------------------------------------------
/ '/'
----------------------------------------------------------------------
\/ '/'
----------------------------------------------------------------------
( group and capture to \1:
----------------------------------------------------------------------
[\w]+ any character of: word characters (a-z,
A-Z, 0-9, _) (1 or more times (matching
the most amount possible))
----------------------------------------------------------------------
(with ^ and $ matching normally) (with . not
matching \n) (matching whitespace and #
normally):
----------------------------------------------------------------------
/ '/'
----------------------------------------------------------------------
\/ '/'
----------------------------------------------------------------------
( group and capture to \1:
----------------------------------------------------------------------
[\w]+ any character of: word characters (a-z,
A-Z, 0-9, _) (1 or more times (matching
the most amount possible))
----------------------------------------------------------------------
) end of \1
----------------------------------------------------------------------
$ before an optional \n, and the end of the
string
----------------------------------------------------------------------
/i '/i'
----------------------------------------------------------------------
) end of grouping
----------------------------------------------------------------------
UPDATE:
See also: https://stackoverflow.com/a/12359682/1015385 . As noted there and in the module's documentation:
There is no support for regular expression syntax added after Perl version 5.6, particularly any
constructs added in 5.10.
/\/([\w]+)$/i;
It is a regex, and if it is a complete statement, it is applied to the $_ variable, like so:
$_ =~ /\/([\w]+)$/i;
It looks for a slash \/, followed by an alphanumeric string \w+, followed by end of line $. It also captures () the alphanumeric string, which ends up in the variable $1. The /i on the end makes it case-insensitive, which has no effect in this case.
While it doesn't help "explain" a regex, once you have a test case, Damian's new Regexp::Debugger is a cool utility to watch what actually occurs during the matching. Install it and then do rxrx at the command line to start the debugger, then type in /\/([\w]+)$/ and '/r' (for example), and finally m to start the matching. You can then step through the debugger by hitting enter repeatedly. Really cool!
This is comparing $_ to a slash followed by one or more character (case insensitive) and storing it in $1
$_ value then $1 value
------------------------------
"/abcdes" | "abcdes"
"foo/bar2" | "bar2"
"foobar" | undef # no slash so doesn't match
The Online Regex Analyzer deserves a mention. Here's a link to explain what your regex means, and pasted here for the record.
Sequence: match all of the followings in order
/ (slash)
--+
Repeat | (in GroupNumber:1)
AnyCharIn[ WordCharacter] one or more times |
--+
EndOfLine

Trying to match what is before /../ but after / with regular expressions

I am trying to match what is before /../ but after / with a regular expressions, but I want it to look back and stop at the first /
I feel like I am close but it just looks at the first slash and then takes everything after it like... input is this:
this/is/a/./path/that/../includes/face/./stuff/../hat
and my regular expression is:
#\/(.*)\.\.\/#
matching /is/a/./path/that/../includes/face/./stuff/../ instead of just that/../ and stuff/../
How should I change my regex to make it work?
.* means "match any number of any character at all[1]". This is not what you want. You want to match any number of non-/ characters, which is written [^/]*.
Any time you are tempted to use .* or .+ in a regex, be very suspicious. Stop and ask yourself whether you really mean "any character at all[1]" or not - most of the time you don't. (And, yes, non-greedy quantifiers can help with this, but character classes are both more efficient for the regex engine to match against and more clear in their communication of your intent to human readers.)
[1] OK, OK... . isn't exactly "any character at all" - it doesn't match newline (\n) by default in most regex flavors - but close enough.
Change your pattern that only characters other than / ([^/]) get matched:
#([^/]*)/\.\./#
Alternatively, you can use a lookahead.
#(\w+)(?=/\.\./)#
Explanation
NODE EXPLANATION
--------------------------------------------------------------------------------
( group and capture to \1:
--------------------------------------------------------------------------------
\w+ word characters (a-z, A-Z, 0-9, _) (1 or
more times (matching the most amount
possible))
--------------------------------------------------------------------------------
) end of \1
--------------------------------------------------------------------------------
(?= look ahead to see if there is:
--------------------------------------------------------------------------------
/ '/'
--------------------------------------------------------------------------------
\. '.'
--------------------------------------------------------------------------------
\. '.'
--------------------------------------------------------------------------------
/ '/'
--------------------------------------------------------------------------------
) end of look-ahead
I think you're essentially right, you just need to make the match non-greedy, or change the (.*) to not allow slashes: #/([^/]*)/\.\./#
In your favourite language, do a few splits and string manipulation eg Python
>>> s="this/is/a/./path/that/../includes/face/./stuff/../hat"
>>> a=s.split("/../")[:-1] # the last item is not required.
>>> for item in a:
... print item.split("/")[-1]
...
that
stuff
In python:
>>> test = 'this/is/a/./path/that/../includes/face/./stuff/../hat'
>>> regex = re.compile(r'/\w+?/\.\./')
>>> regex.findall(me)
['/that/..', '/stuff/..']
Or if you just want the text without the slashes:
>>> regex = re.compile(r'/(\w+?)/\.\./')
>>> regex.findall(me)
['that', 'stuff']
([^/]+) will capture all the text between slashes.
([^/]+)*/\.\. matches that\.. and stuff\.. in you string of this/is/a/./path/that/../includes/face/./stuff/../hat It captures that or stuff and you can change that, obviously, by changing the placement of the capturing parens and your program logic.
You didn't state if you want to capture or just match. The regex here will only capture that last occurrence of the match (stuff) but is easily changed to return that then stuff if used global in a global match.
NODE EXPLANATION
--------------------------------------------------------------------------------
( group and capture to \1 (0 or more times
(matching the most amount possible)):
--------------------------------------------------------------------------------
[^/]+ any character except: '/' (1 or more
times (matching the most amount
possible))
--------------------------------------------------------------------------------
)* end of \1 (NOTE: because you're using a
quantifier on this capture, only the LAST
repetition of the captured pattern will be
stored in \1)
--------------------------------------------------------------------------------
/ '/'
--------------------------------------------------------------------------------
\. '.'
--------------------------------------------------------------------------------
\. '.'