Following code snippet shows how can we use back reference to remove duplicate word from string.
var test = "personal personal computers and super computers computers";
test = test.replace(/(\w+)\s+\1/g,"$1");
alert(test);
Thanks to Cracki for pointing out difference between back reference and back tracking.
Blogs on .NET and LAMP Technologies
There's often more than one correct thing.
There's often more than one right thing.
There's often more than one obvious thing.
--Larry Wall
Showing posts with label Regex. Show all posts
Showing posts with label Regex. Show all posts
Wednesday, May 27, 2009
Sunday, March 15, 2009
Regular Expression Cookbook

I have been waiting for this Regex cookbook for long time. I will make sure that I am the first person to get this book from store as soon as it is available.
http://www.amazon.com/gp/product/0596520689?ie=UTF8&tag=slfb-20&linkCode=as2&camp=1789&creative=390957&creativeASIN=0596520689
Friday, January 09, 2009
Some common regexs
set COMMENT {/\*[^*]*\*+([^/*][^*]*\*+)*/} # regex to match a comment
set COMMENT2 "//\[^\n]*" # regex to match a C++// comment
set DOUBLE {"(\\.|[^"\\])*"} # regex to match doublequoted string
set SINGLE {'(\\.|[^'\\])*'} # regex to match singlequoted constant
Thursday, October 16, 2008
Using Resource files in Web Service (.NET 3.5) and Regular expression with LINQ
/*
Those who has tried to use resource file in ASP.NET web service know well there is issue in using resource files in web service. Following code snippet targets 3 points:
1. Using resource file in web service or HttpHandlers.
2. Regular expression for converting resource strings with respective localized strings
3. LINQ integration in Regular expression.
Suppose placeholder for resource string in text is like ({BR[MyResourceString]BR}) . Following function will return localized string.
*/
public static string LocalizedHTML(string inputHtml)
{
if (string.IsNullOrEmpty(inputHtml)) return "";
Regex reg = new Regex(@"\(\{IA\[(.*?)\]IA\}\)", RegexOptions.Multiline | RegexOptions.Singleline
| RegexOptions.IgnoreCase | RegexOptions.Compiled);
System.Resources.ResourceManager RM =
new System.Resources.ResourceManager("Resources.Language",
global::System.Reflection.Assembly.Load("App_GlobalResources"));
return reg.Replace(inputHtml, m => (!string.IsNullOrEmpty(RM.GetString(m.Groups[1].Value)) ? RM.GetString(m.Groups[1].Value) : m.Value));
}
Those who has tried to use resource file in ASP.NET web service know well there is issue in using resource files in web service. Following code snippet targets 3 points:
1. Using resource file in web service or HttpHandlers.
2. Regular expression for converting resource strings with respective localized strings
3. LINQ integration in Regular expression.
Suppose placeholder for resource string in text is like ({BR[MyResourceString]BR}) . Following function will return localized string.
*/
public static string LocalizedHTML(string inputHtml)
{
if (string.IsNullOrEmpty(inputHtml)) return "";
Regex reg = new Regex(@"\(\{IA\[(.*?)\]IA\}\)", RegexOptions.Multiline | RegexOptions.Singleline
| RegexOptions.IgnoreCase | RegexOptions.Compiled);
System.Resources.ResourceManager RM =
new System.Resources.ResourceManager("Resources.Language",
global::System.Reflection.Assembly.Load("App_GlobalResources"));
return reg.Replace(inputHtml, m => (!string.IsNullOrEmpty(RM.GetString(m.Groups[1].Value)) ? RM.GetString(m.Groups[1].Value) : m.Value));
}
Monday, May 26, 2008
.NET Regex Balanced Grouping
Thursday, December 14, 2006
Regex for atleast one alphabet and one number in string
Tuesday, December 05, 2006
Wednesday, December 28, 2005
Regular Expression: Multiple replacement in one go
One of the overloaded form of Regex.Replace function takes MatchEvaluator delegate as a parameter. We can use this form to achieve multiple (conditional) replacement in one go.
For Example:
For Example:
// Replace Textboxes
string regex = "\\";
System.Text.RegularExpressions.RegexOptions options = ((System.Text.RegularExpressions.RegexOptions.IgnorePatternWhitespace
| System.Text.RegularExpressions.RegexOptions.Multiline)
| System.Text.RegularExpressions.RegexOptions.IgnoreCase);
System.Text.RegularExpressions.Regex reg = new System.Text.RegularExpressions.Regex(regex, options);
pageContent = reg.Replace(pageContent,new MatchEvaluator(RemoveFormElement));
protected string RemoveFormElement(Match m)
{
if(m.Value.IndexOf("RemoveFeedback") != -1)
{
string feedback = Regex.Replace(txtFeedback.Text,"\\n","
");
return ""+feedback+"";
}
else if (m.Value.IndexOf("RemoveContextHeader") != -1)
{
return ""+ contextHeader +"";
}
else
{
return m.Value;
}
}
Subscribe to:
Posts (Atom)

