Replace text help

Hey JM,

My somewhat more robust pattern:

^([[:alpha:]]+://.+?[?&]mod=)(.+?)(:.+)

Detailed examination:

^		Beginning of line
(		Start capture
[[:alpha:]]	Alphabetic characters
+		One or more
://		Literal character sequence
.+?		Any character 1 or more non-greedy
[?&]		Either “?” or “&”
mod=		Literal character sequence
)		End capture
(		Start capture
.+?		Any character 1 or more non-greedy
)		End capture
(		Start capture
:		Literal character
.+		Any character 1 or more greedy
)		End capture

To analyze in on regex101.com you have to adjust the pattern slightly by escaping the two forward slashes and adding the ‘m’ switch.

^([[:alpha:]]+:\/\/.+?[?&]mod=)(.+?)(:.+)

Forward slashes are used by regex101 as pattern delimiters - i.e. they delimit the beginning and the end of the pattern – and this is why they must be escaped in the pattern for regex101.com.

You must realize the analyzer is representing code-based RegEx usage like in Perl, JavaScript, others… – and NOT a GUI-based search/replace mechanism. This is confusing if you've never seen a programming language do regular expressions.

Perl search/replace syntax looks like this:

s/<search-pattern>/<replace-pattern>/<switches>;

An actual working Perl script that replaces every character (.) with a bullet (•).

#! /usr/bin/env perl -sw

my $strVar = "Now is the time for all good men to come to the aid of their country.";

$strVar =~ s/./•/g;

print $strVar;

Seeing actual examples of sed, awk, and Perl give this business of delimiters and switches context that helps users understand instead of just memorize.

Back to the pattern – here it is on regex101.com:

Here it is with free-spacing turned on and comments:

Generally I prefer to use a RegEx analyzer app on my system to the online ones, but of course that's more difficult to share.

RegExRX is one of the better inexpensive RegEx analyzers. ($4.99 direct or from the app-store, and there's a demo.)

It doesn't give you the comprehensive explanation regex101.com does, but you don't have to fool with the pseudo code separators and such in //.

I think this makes it a little closer to what the user sees in GUI-based systems.

The best analyzer I'm acquainted with is the Windows App RegexBuddy.

(When I upgrade my hardware next time (and have more memory) I'll be able to use Parallels and will buy a copy despite the fairly hefty price – $39.95 U.S.)

-Chris

1 Like

thanks for the cheat sheet @ccstone :+1:

Here's another online RegEx cheat sheet I like:
http://www.rexegg.com/regex-quickstart.html#ref

2 Likes