!cat zen.txt
%%script perl
open (ZENFILE, 'zen.txt');
while (<ZENFILE>) {
print if /never/;
}
%%script perl
open (ZENFILE, 'zen.txt');
while (<ZENFILE>) {
print if /is better than/;
}
%%script perl
open (ZENFILE, 'zen.txt');
while (<ZENFILE>) {
print if /simple/;
}
%%script perl
open (ZENFILE, 'zen.txt');
while (<ZENFILE>) {
print if /simple/i;
}
%%script perl
open (ZENFILE, 'zen.txt');
while (<ZENFILE>) {
print if / ... /;
}
%%script perl
open (ZENFILE, 'zen.txt');
while (<ZENFILE>) {
print "$1\n" if / (...) /;
}
%%script perl
open (ZENFILE, 'zen.txt');
while (<ZENFILE>) {
if (/(....) to (.....)/) {
print;
print "$1, $2\n";
}
}
*
means "any number"¶%%script perl
open (ZENFILE, 'zen.txt');
while (<ZENFILE>) {
if (/(.*) to (.*)/) {
print;
print "$1, $2\n";
}
}
%%script perl
open (ZENFILE, 'zen.txt');
while (<ZENFILE>) {
print "$1\n" if / ([aeiou].....) /i;
}
%%script perl
open (ZENFILE, 'zen.txt');
while (<ZENFILE>) {
print "$1\n" if / ([aeiou][^ ]*) /i;
}
%%script perl
open (ZENFILE, 'zen.txt');
while (<ZENFILE>) {
if (/([^ ]*) to ([^ ]*)/) {
print;
print "$1, $2\n";
}
}
Hmm. How to get rid of those trailing punctuations?
%%script perl
open (ZENFILE, 'zen.txt');
while (<ZENFILE>) {
if (/([a-z]*) to ([a-z]*)/) {
print;
print "$1, $2\n";
}
}
%%script perl
open (ZENFILE, 'zen.txt');
while (<ZENFILE>) {
if (/([\w]*) to ([\w]*)/) {
print;
print "$1, $2\n";
}
}
%%script perl
open (ZENFILE, 'zen.txt');
while (<ZENFILE>) {
$_ =~ s/is/might be/;
print $_;
}
%%script perl
open (ZENFILE, 'zen.txt');
while (<ZENFILE>) {
$_ =~ s/ (\w*) to / \1, really very \1, to /;
print $_;
}
%%script perl
open (ZENFILE, 'zen.txt');
while (<ZENFILE>) {
if (/ to /) {
print
print split / /;
}
}
%%script perl
$data = "I can be reached at (937) 395-2343 or 122-4235, unless it's raining."
if ($data =~ /\d+-\d+/
This flag allows you to write regular expressions that look nicer. Whitespace within the pattern is ignored, except when in a character class or preceded by an unescaped backslash, and, when a line contains a '#' neither in a character class or preceded by an unescaped backslash, all characters from the leftmost such '#' through the end of the line are ignored.
%%script perl
open (ZENFILE, 'zen.txt');
while (<ZENFILE>) {
if (/(\b[A-Z][a-z]+\b)/) {
print $1;
print "\n";
}
}
Random question: How to display all the capitalized words?
But anyway:
%%script perl
open (ZENFILE, 'zen.txt');
while (<ZENFILE>) {
if (/ # Capitalized word detector
( # capture the result in $1
\b # word boundary
[A-Z] # one capital letter
[a-z]+ # one or more lowercase letters
\b # word boundary
) # end captured group
/x) {
print $1;
print "\n";
}
}
see wiki
from verbalexpressions import VerEx
verbal_expression = VerEx()
# Create an example of how to test for correctly formed URLs
verbal_expression = VerEx()
tester = (verbal_expression.
start_of_line().
find('http').
maybe('s').
find('://').
maybe('www.').
anything_but(' ').
end_of_line()
)
tester.source()
tester.match("https://www.google.com")
tester.match("my nifty website")
result = tester.match("https://www.google.com")
result.groups()
VerEx().anything().source()
VerEx().find('cows').match('how do you like Cows?')
ve2 = VerEx().find("ftp").or().find("http").maybe("s").then("://")
see comparison
Tragically, they haven't yet set it up for pip install
. I cloned from git & then created a symlink:
ln -s ~/sw/RegExpBuilder/python/regexpbuilder regexpbuilder
from regexpbuilder.RegExpBuilder import RegExpBuilder
builder1 = (RegExpBuilder()
.find("$")
.min(1).digits()
.then(".")
.digit()
.digit())
builder1.search("is there money in $2.22?")
money = builder1.get_regexp()
result = money.search("is there money in $2.22?")
result
result.group(0)
discount = (RegExpBuilder()
.find("was").then(money)
.maybe(",").find("now").then(money)
)
money
type(money)