simple intro to regular expressions (perl style):
http://www.perl.com/doc/manual/html/pod/perlre.html
http://tnx.nl/php
http://etext.lib.virginia.edu/helpsheets/regex.html
http://www.anaesthetist.com/mnm/perl/regex.htm
»
http://perl.sioc.org/win32perltut.html
http://www.sthomas.net/roberts-perl-tutorial.htm
http://www.physics.rutgers.edu/~kotliar/perltut.html
«
http://www.perlmonks.org/index.pl?node=perlre&lastnode_id=5485
http://www.techtutorials.info/perlgen.html
...
cgi: http://stason.org/TULARC/webmaster/myfaq.html
wm: http://hotwired.lycos.com/webmonkey/98/47/index2a.html
? http://my.execpc.com/~keithp/bdlogcgi.htm
regular expressions with JavaScript
make all tags in 'var' lowercase:
// have not confirmed this will permit newlines but it's a start
// also does not permit embedding html in attributes (stops at first > it finds)
var.replace(/(<\/?[A-Z0-9]+?)([^>]*?>)/g, function(a,b){ return a.toLowerCase() + b; });
// this returns <B> and </B> as <b> and </b>
var.replace(/(<\/?[A-Z0-9]+?)(?:[^>]*?>)/g, function(a){ return a.toLowerCase(); });
// like previous. these need testing to prove
every string without an apple.com subdomain quicktime .mov file
/https?:\/{2}(?![a-zA-Z_0-9\-]+\.apple\.com\/)[\/a-zA-Z0-9_\-]+?\.(?:mov)/
The intention is to adblock all quicktime movies outside of apple.com subdomains.
quote attributes
str.replace(/(=)([^'"]+?)([\s>])/g, '$1"$2"$3')
trim
str.replace(/^\s*(\S*(\s+\S+)*)\s*$/, '$1')
regular expressions on the command line
find all files in and below current dir with extension .psd--skipping '._' prefixed files (mac osx)--and put them into a tar and gzip'ed file backup.tar.gz
tar -cvf - `find . -name "*.psd" -print | grep -i '.*\/\w*\.psd$'` | gzip -9 > backup.tar.gz
related: restore/expand all files from this same file (tar will put these all back where they were, or in a matching dir tree if these directories don't exist from the pwd):
zcat backup.tar.gz | tar -xvf -
regular expressions with VIM
http://www.geocities.com/volontir/
http://www.tldp.org/HOWTO/Vim-HOWTO.html#toc13
http://www.tldp.org/HOWTO/Vim-HOWTO-13.html
^ and $ start and end of line, respectively (you can insert characters at these points, but not remove a line with this syntax)
search thu a file replacing pattern ## with a newline:
:%s/##/\r/g
same pattern only current line:
:s/##/\r/g
same pattern only lines 10 to 40:
:10,40s/##/\r/g
same pattern only current line to end of file:
:.,$s/##/\r/g
change first letter to uppercase from the current line thru the next 4 lines:
:.,+4s/\w\([a-z]\)/\u\1/g
change all-cap links (a href=...>LINK<...) to lowercase:
:2,10s/>.*/\L&/g
change first letter to capital:
:2,10s:\([> ]\)\([a-z]\):\1\u\2:g
join all lines in a file (so there is only a single line break)
:1,$j
substitue > character with > on lines 38 to the end of the file:
:38,46s/>/\>/g
make word characters lowercase on lines 11 to 37:
:11,37s/\(\w\)/\l\1/g
non greedy pattern use \{-} after pattern definition
eg (for stylesheet editing) we simply insert a font tag:
selector{rule}
selector{font:10px verdana;rule}
find this spot: /{.\{-}}
insert globally: :%s/{\(.\{-}\)}/{font:10px verdana;\1}/g
non-case sensitive follow with \c search:
/f\c
:%s/s\c/S/g
multi-file search and replace (search and replace all open buffers):
:bufdo! %s/http:..www.\{-}path\///g
from the current working directory edit all files named *.html, then search
and replace write and exit each of the buffers (seems to hang on last buffer
in gvim, not sure why)
:args *.html
:bufdo! %s/href=\"g/href=\"/ge
see also: http://www.vim.org/tips/tip.php?tip_id=373
regular expressions in homesite
pair with multi-file capabilities and it's quite efficient
http://www.google.com/search?q=regular+expressions+homesite
http://answers.google.com/answers/main?cmd=threadview&id=214551
<br><p><img src=mypic.jpg>My Summer home called Foo House</p> <hr>
we want this replaced to read :
<p>My Summer home. The Foo House</p>
To do this with a regular expression
[A-Za-z0-9_ ><.="']*Foo
-moz-appearance: [^none^#chr(13)##chr(10)#]
-moz-appearance: ^(none)([A-Za-z]|-);
("-moz-appearance: ","^(none)([A-Za-z]|-)+")
-moz-appearance: ([A-Za-z]+)
-moz-appearance: ([A-Za-z]|-)+;
(-moz-appearance: )(([A-Za-z]|-)+;)
\1 yo \2
\1 matches first group in (): (-moz-appearance: )
\2 matches second: (([A-Za-z]|-)+;)
script that will find a string from the current working directory
#!/path/to////perl
# perl-only rewrite (Zephyr's previous version below)
# not the best, but perhaps it'll be useful
my $start = time();
my $recursive = 0;
my $ignorecase = 1;
my $isregex = 0;
my @matches;
my @resultfilelist;
my $filecount = 0;
while($_ = shift @ARGV){
if(/^-r$/){ # can do "-r" matches as long as they contain more characters
$recursive = 1;
}elsif(/^-p$/){
$isregex = 1;
}elsif(/^-c$/ || /^-I$/){
$ignorecase = 0;
}else{
push(@matches, $_);
}
}
if(@matches == 0){
print qq~
Search text files from the current directory for literal or regex patterns.
Options:
-r recursive (default: off)
-I case-sensitive (default: off)
-c same as -I
-p patterns are regular expressions, not literal strings (default: off)
search all text-files in current directory for "html":
findit html
search for "<html class=":
findit "<html class="
recursively search for strings "blah" and "class":
findit -r blah class
search for case-sensitive string "blah className" recursively:
findit -r -I "blah className"
search for regular expression pattern recursively:
findit -r -p "\d:[01]{2}\s";
~;
exit 0;
} # no matches
if(!$isregex){
for(my $i=0;$i<@matches;$i++){
# replace all the literals for passing into the regex later
$matches[$i] =~ s/[\/.*[()\$?+\{|]/\\$&/g;
$matches[$i] =~ s/^\^/\\$&/;
if($ignorecase){ $matches[$i] = '(?i:'.$matches[$i].')'; }
}
};
print "".($recursive ? "recursively ":"")."________searching for \"".join('", "', @matches)."\"\n";
search(".");
print(qq~
___
___
___
___
___
___
___
________________________________________________________
________search completed for "~,
join('", "', @matches),
"\" found in ", scalar @resultfilelist, " files:\n",
, join("\n", @resultfilelist), qq~
________~, (time() - $start), " seconds to find matches in ", scalar @resultfilelist, " of $filecount files\n");
exit 0;
sub search{
my ($dir) = @_;
my @results;
my @files = dir($dir);
FILELOOP: foreach $file(@files){
# if dir && recursive push in the dir's results (call &search(<DIR>)!)
stat($file);
if(-d _ && $recursive){
# just check the subfolder, don't do anything with it directly
push(@results, search($file));
}elsif(-T _){
# look in the file for matches
next unless open(FILE, $file);
my $resultcount = 0;
$filecount++;
LINE: while($line = <FILE>){
my $count = 0;
# TODO dynamic case set or regex (I need to find out how todo this)
foreach(@matches){ $count++ if $line =~ m/$_/o; }
next unless ($count > 0);
if($resultcount == 0){
print "\n________finding matches in $file\n";
push(@resultfilelist, $file);
}
$resultcount+=$count;
print "$line";
}
print "________$resultcount matches in $file\n" if $resultcount;
close FILE;
}
}
return @results;
}
sub dir{ # a single dir's content
my ($dir) = @_;
my @listing;
opendir(DIR, $dir) or warn "cannot open $dir: $!\n";
while(defined($file = readdir(DIR))){
next if $file =~ m/^\.{1,2}$/;
$file = "$dir/$file";
stat($file); # file test operators perl book p98
next unless -r _ || -s _;
if(-d _){ # dir
push(@listing, $file);
}elsif(-T _){ # regular text file
unshift(@listing, $file);
}
}
closedir(DIR);
return @listing;
}
#!/usr/local/bin/perl
# this recursively grep's for your argument then sends it to the shell
#
# usage is: path/to/this.script argument
# example (command aliased): findthis word
# returns all the file names with 'word' in them including a sample of the file
#
# see the man pages for grep or your shell for more info
open( FIND, "find . -type f|" );
map{ $search .= $_." " } @ARGV;
$search =~ s/\s$//;
while( <FIND> ) {
chomp($_);
$in = `grep -in "$search" $_`;
( $in !~ /^\s*$/ ) && print "$_\n$in\n_____________________________________\n";
}
close( FIND );
exit;