Tag Archive for regular

Regular Expressions

using System;
using System.Text.RegularExpressions;

namespace regexpressions
{
class Program
{
static void Main(string[] args)
{
string content = @"xyz <a href="http://www.xyz-xyz.com" >http://www.xyz-xyz.com</a> abc <a href="http://www.abc.org" >http://www.abc.org</a> def <a href="https://www.def.net" >https://www.def.net</a> vwx <a href="ftp://fileserver.vwx.org/dot/net";" >ftp://fileserver.vwx.org/dot/net";</a>
string pattern = @"S{1,5}://S+.S{2,}"; //not a good url-pattern - only for demo

//more matches - find the urls
MatchCollection Matches = Regex.Matches(content, pattern, RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture);

foreach (Match match in Matches)
{
Console.WriteLine(match.ToString());
}

//one match - find only one (the first) url
Match oneMatch = Regex.Match(content, pattern, RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture);

Console.WriteLine(oneMatch.ToString());
}
}
}

source

Remove Leading Whitespace (blanks) with Perl

$emailaddress =~ s/^s+//;

source

Remove HTML with Perl Regular Expression

$cleancode =~ s/<(.*?)>//gi;

source

Get Input Controls Name And Value In HTML Page

public static string GetInputControlsNameAndValueInPage(string strPage)
{
string strRegExPatten = "<s*input.*?names*=s*"(?<Name>.*?)".*?values*=s*"(?<Value>.*?)".*?>";
Regex reg = new Regex(strRegExPatten, RegexOptions.Multiline);
MatchCollection mc = reg.Matches(strPage);
string strTemp = string.Empty;
foreach (Match m in mc)
{
strTemp = strTemp + m.Groups["Name"].Value + "=" + m.Groups["Value"].Value + "&";
}
int n = strTemp.Length;
strTemp = strTemp.Remove(n - 1);
return strTemp;
}

source

Mexican RFC RegEx

^[A-Za-z]{4}-d{6}(?:-[A-Za-zd]{3})?$

source

One liner perl extract content with regular expression

curl -s <a href="http://checkip.dyndns.org" >http://checkip.dyndns.org</a> | perl -nle 'print "$1" if (/Current IP Address: ([d.]*)/)'

source

Regular expressions filename replace

// This will strip out any punctuation and spaces from filenames, replacing such characters with underscores
$filename = preg_replace("/[^a-zA-Z0-9s.]/", "_", $filename);

source

Regular Expressions 3/3

//pattern examples. Creating a time pattern that can be reused.
def timePattern = ~/dd:dd/

def timeString = "Between 06:00 and 10:00, most geeks are asleep."
def matcher = timePattern.matcher(timeString)
def times = []
matcher.each { times << it }
assert times.join(", ") == '06:00, 10:00'

def anotherTimeString = "But between 22:00 and 02:00, most geeks are awake"
//we reuse the pattern
matcher = timePattern.matcher(anotherTimeString)
times = []
matcher.each { times << it }
assert times.join(", ") == '22:00, 02:00'

source

Regular Expressions 2/3

//examples for match operator - match is more restrictive than find!
def line = "This is a test line"
assert line ==~ /This is a test line/
line -= " line"
assert !(line ==~ /This is a test line/)
def booleanValue = (line ==~ /This is a test/)
assert booleanValue == true

source

Regular Expressions 1/3

//regex patterns look strange, but groovy makes them look better
//use slashy syntax to declare a pattern, get rid of double-esacping
//example below both defines the same pattern to match a time like 15:01
assert "dd:dd" == /dd:dd/

//find a string, return true if found, false otherwise
assert 'The time is 15:01' =~ /dd:dd/

//note, you can easily use this for branching
def timeString = 'It is now 17:34'
def timeRegex = /dd:dd/
if (timeString =~ timeRegex) assert true
else assert false

//calling matcher.find() finds the first occurrence, then steps to the next if called again
//notice we used a regex group in the pattern: /(dd:dd)/
def matcher = ("It is 15:01, it is 15:02, it is 15:03" =~ /(dd:dd)/ )
assert matcher instanceof java.util.regex.Matcher
def times = []
while (matcher.find())
{
times << matcher.group() //this adds each match to the list
}
assert times.size() == 3
assert times.join(",") == "15:01,15:02,15:03"

//a more groovy way to the same thing as above, eachMatch method in String
//note: no grouping parentheses needed in regex! /dd:dd/
times = []
"It is 15:01, it is 15:02, it is 15:03".eachMatch(/dd:dd/) {
times << it[0] //it is a list, position 0 contains the complete match
}
assert times.size() == 3
assert times.join(",") == "15:01,15:02,15:03"

//using groups with the matcher and each closure. Within the pattern, we can use GString replacements to make
//the pattern readable
def DATE = /dd.dd.dddd/
def TIME = /dd:dd/
def dates = []
("Today is 31.01.2007, 15:01. Tomorrow is 01.02.2007, 15:01" =~ /($DATE), ($TIME)/).each { all, date, time ->
dates << date
}
assert dates.size() == 2
assert dates.join(', ') == '31.01.2007, 01.02.2007'

source