Username:   Password:  

Verify email address with Javascript

function checkMail(email){
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (filter.test(email)) {
		return true;
	}
	return false;
}

Verifies, if input is a valid email address with Javascript by using regular expressions

Tags

regex email verify verification address JS Javascript

Search and replace string recursively in C#

static void ProcessDir(string path, string search, string replace)
{
    // First access subdirectories
    string[] subDirs = Directory.GetDirectories(path);
 
    foreach (string curSubDir in subDirs)
    {
        ProcessDir(Path.Combine(path, curSubDir));
    }
 
    // Now process files
    string[] files = Directory.GetFiles(path, "*.txt");
 
    foreach(string curFile in files) {
        ProcessFile(Path.Combine(path, curFile), search, replace);
    }
}
 
private static void ProcessFile(string path, string search, string replace)
{
    StreamReader input = new StreamReader(path);
    string content = input.ReadToEnd();
    input.Close();
 
    content = Regex.Replace(content, search, replace);
 
    try
    {
        StreamWriter output = new StreamWriter(path);
        output.Write(content);
        output.Close();
    }
    catch (IOException)
    {
        Console.WriteLine("Skipping file " + path);
        return;
    }
}

Searches recursively for *.txt files in a directory and subdirectories and replaces all occurences of "string search" with "string replace"

Tags

search replace C# CSharp .NET Regex Regual Expressions

Find out if string is serialized variable

<?php
 
$string = "a:0:{}";
if(preg_match("/(a|O|s|b)\x3a[0-9]*?
((\x3a((\x7b?(.+)\x7d)|(\x22(.+)\x22\x3b)))|(\x3b))/", $string))
{
echo "Serialized.";
}
else
{
echo "Not serialized.";
}
 
?>

If you need to check whether string is a serialized representation of variable(sic!) you can use this. But don't forget, string in serialized representation could be VERY big, so match work can be slow, even with fast preg_* functions.

Tags

regex PHP preg_match serialization

List all files that match this regular expression

ls | grep -e .*5.*
 

Lists all files with the number 5 in their name.

Tags

regex bash

Remove non alphanumeric characters

<?php
    $output = preg_replace('/[^[:alnum:]]/', '', $input);
?>

Replaces everything, thats not alphanumeric in a string. Note that spaces are not alphanumeric, too.

Tags

php regex preg_replace