Tag Archive for regex

Get URL Parameters

function gup( name )
{
name = name.replace(/[[]/,"[").replace(/[]]/,"]");
var regexS = "[?&]"+name+"=([^&#]*)";
var regex = new RegExp( regexS );
var results = regex.exec( window.location.href );
if( results == null )
return "";
else
return results[1];
}

source

regex to trim data from beginning of a line

^.*s(PM){1}s*

source

Trim TAB/CR/LF/Space from string

SELECT REGEXP_REPLACE(
'string'
, '^([[:space:]]|'||CHR(9)||'|'||CHR(10)||'|'||CHR(13)||')+|([[:space:]]|'||CHR(9)||'|'||CHR(10)||'|'||CHR(13)||')+$', '*') AS result
FROM dual;

source

all spaces become single spaces regex

$test = '23       124 124     4    2  21 4  142    lajdlkajsldkajs                              jasd    124  what   great  data t a  a';
$test = preg_replace( '([ ])', ' ', $test );
echo $test;

source

Apache regex

regex = /^(pms(?!/attachments).*)$/
patterns = ["pms", "pms/attachments", "pms/asdf", "pms/attachments/stuff.txt", "pms/asdf/werqw.txt"]
patterns.each do |p|
matches = p.match(regex)
if matches
puts "? = [#{matches}]"
end
end
nil

source

Make page titles seo-friendly for URL

function make_seo_name($title) {
return preg_replace('/[^a-z0-9_-]/i', '', strtolower(str_replace(' ', '-', trim($title))));
}

source

Strip phone number of all non alpha-numeric characters

<?php

/**
* Strip a Phone number of all non alphanumeric characters
*/
$phone = "+1(555)-555-1212";
$phone = preg_replace('/(W*)/', '', $phone);
print $phone;

// Result: 15555551212

?>

source

Regular Expressions for U.S. Phone Numbers

//10-digit phone number
^(?:+?1s*(?:[.-]s*)?)?(?:(s*([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9])s*)|([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9]))s*(?:[.-]s*)?([2-9]1[02-9]|[2-9][02-9]1|[2-9][02-9]{2})s*(?:[.-]s*)?([0-9]{4})$

//10-digit phone number, extension allowed
^(?:+?1s*(?:[.-]s*)?)?(?:(s*([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9])s*)|([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9]))s*(?:[.-]s*)?([2-9]1[02-9]|[2-9][02-9]1|[2-9][02-9]{2})s*(?:[.-]s*)?([0-9]{4})(?:s*(?:#|x.?|ext.?|extension)s*(d+))?$

//7 or 10-digit phone number
^(?:(?:+?1s*(?:[.-]s*)?)?(?:(s*([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9])s*)|([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9]))s*(?:[.-]s*)?)?([2-9]1[02-9]|[2-9][02-9]1|[2-9][02-9]{2})s*(?:[.-]s*)?([0-9]{4})$

//7 or 10-digit phone number, extension allowed
^(?:(?:+?1s*(?:[.-]s*)?)?(?:(s*([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9])s*)|([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9]))s*(?:[.-]s*)?)?([2-9]1[02-9]|[2-9][02-9]1|[2-9][02-9]{2})s*(?:[.-]s*)?([0-9]{4})(?:s*(?:#|x.?|ext.?|extension)s*(d+))?$

//10-digit phone number, vanity (alphanumeric) allowed
^(?:+?1s*(?:[.-]s*)?)?(?:(s*([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9])s*)|([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9]))s*(?:[.-]s*)?([2-9a-z]1[02-9a-z]|[2-9a-z][02-9a-z]1|[2-9a-z][02-9a-z]{2})([0-9a-z]{4})$

//10-digit phone number, vanity allowed, extra characters allowed at end
^(?:+?1s*(?:[.-]s*)?)?(?:(s*([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9])s*)|([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9]))s*(?:[.-]s*)?([2-9a-z]1[02-9a-z]|[2-9a-z][02-9a-z]1|[2-9a-z][02-9a-z]{2})([0-9a-z]{4})([0-9a-z]*)$

source

nested groups

import re

def replace(match, withwhat):

starttext = match.group(0)
n = len(withwhat)
off = match.start(0)

tmp = starttext
for i in range(n):
delta = len(tmp)-len(starttext)
tmp = tmp[:match.start(i+1)-off+delta ] + withwhat[i](match.group(i+1)) + tmp[match.end(i+1)-off+delta:]
return tmp

#test
funcs = [lambda x: '1'+x+'1',lambda x: '2'+x+'2']
#regex contains 2 capturing groups
print re.sub(regex, lambda x: replace(x, funcs), text)

source

Match Single-line Javascript Comments

Regexp.new(///.*$/)

source