Click here to Skip to main content
15,881,871 members
Everything / General Programming / Regular Expressions

Regular Expressions

regular-expression

Great Reads

by Christoph Buenger
Describes PHP application development with the free Scavix Web Development Framework (Scavix-WDF).
by honey the codewitch
Build a feature rich, non-backtracking regular expression engine and code generator in C#
by honey the codewitch
Embed fast streaming C# code to match text based on inputted regular expressions
by Daniel Vaughan
A Visual Studio regex to remove someone's overzealous use of regions in VS. Find and replace: (^.*\#region.*$)|(^.*\#endregion.*$) Remember to enable regular expressions in the Visual Studio find and replace dialog.

Latest Articles

by honey the codewitch
Easily add lexers to your project with this simple drop in NuGet package and a few annotations
by honey the codewitch
In this article, we explore the inner workings of Visual FA
by honey the codewitch
Continuing in the Visual FA series, now we explore API fundamentals
by honey the codewitch
In this article, we will use my Visual FA solution to explore finite automata concepts used for finding patterns in text.

All Articles

Sort by Updated

Regular Expressions 

4 Dec 2010 by #realJSOP
I wrote an article about string splitting you might be interested in. It wouldn't do exactly what it looks like you need, but it might provide a starting point.Persistent String Parser[^]
6 Dec 2010 by #realJSOP
If you're using .Net, you can probably load the javascript into a BrowserWindow, and then use the DOM to extract the method names (let the DOM do the parsing for you). How you would specifically go about doing this, I leave to you as a programming exercise.
11 Jan 2011 by #realJSOP
It's certainly the fastest way to type it.If you want to try to find faster ways, be a programmer and try different ways to approach the problem.Performance for such a task would greatly depend on the length of the string involved.
20 Jan 2011 by #realJSOP
You might want to consider using the HTMLAgilityPack:HTMLAgilityPack at Codeplex[^]I don't know for sure if there's a method in that library that fixes missing tags HTML, but you might want to check it out.
19 Apr 2011 by #realJSOP
Why use a regular expression? You didn't specify a language, so the following is in C#:var count = (from c in myString where c == ' ' select c).Count();valid = (count == 2 || count == 0);Depending on the environment/context, I'd consider wait until it's...
26 May 2011 by #realJSOP
I wrote an article for this, but it doesn't use Regex.Persistent String Parser[^]
8 Jun 2011 by #realJSOP
Well, I would first "normalize" the string so that you're always woirking with the data delimited same same way:0) Call string.Replace(", ", "")1) Traverse the string looking for a right bracket (']') character. 3) If you find one, get the substring comprised of every character up to...
9 Sep 2011 by #realJSOP
Why use regex? // I would personally make this an extension methodpublic static string ReplaceOccurrence(this string str, string target, string replacement, int count){ int pos = -1; int counted = 0; string result = ""; do { pos = (pos >= 0) ?...
18 Oct 2016 by #realJSOP
0) I think your email regex is rather weak. Try this one:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?1) Why not just split the string on spaces, regex the email address, and then run the phone number...
4 May 2021 by - Uli Minotti
Hello, I have a simple question in regex. I need to get the first 3 characters of that string ("newtest-test"), the dash and the 2 characters after the dash (like that "new-te"). I use this regex "\w{3}\-\w{2}" but return "est-te". What's...
1 Dec 2014 by /\jmot
See here..The 30 Minute Regex Tutorial[^]
19 Oct 2011 by 01.mandar
i have written Bluetooth code in win32 which detected bluetooth devices in range andafter installing bluetooth device on my PC it creates more than one COM portto get serial port i search in HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSetbut some time the newly installed bluetooth do not...
1 Jul 2014 by 204.sharma
Hi, I want to create a regular expression to search a pattern. My search pattern is 4 alphanumeric / 5 alphanumeric / 2 alphanumeric. like (AP11/0258A/12).i am using this const string Pattern =...
5 May 2011 by 555336
Hello, I have a massive problem in regular expression in search. Well I have a textbox and a button, here the code I put in the button event for the search by using regular expression:The problem I'm having is, I want search a mistyped word from letter A to Z both in lower and upper-cases....
22 Aug 2017 by @BangIndia
I have string with is having the alpha numeric noted as below. Ex 1: abc1232323acvc23232avac Ex 2: abc1232bcsb Here I want to check the string is alpha number as well as continuous numeric length should not exceed more than 7. I have tried but I couldn't get the proper regular expression. ...
6 May 2014 by _Asif_
Try below one. ^[A-Za-z0-9]{6,15}$-Asif
9 Sep 2014 by _Asif_
Try this(?=^.{8,}$)(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?!.*\s)[0-9a-zA-Z@\-]*$Look RegExLib[^] for other examples
1 Jan 2015 by _Asif_
How could i parse below text identifying xml tags using regular expression.Dear This is to let you know that your following Tickets have been confirmed. Total Amount is Thanks for doing business with...
26 May 2015 by _Asif_
Try this "\d+(\.\d+)?". You can find plenty of examples over the net. Just Google!
4 Feb 2018 by A_Griffin
I need a regex - well, two: I want a JS regex to replace all whitespace with a hyphenvar str = 'some string'; var regex1 = ?????; str = str.replace(regex1, '-'); and another to replace all non-alphanumerics with nothing - ie remove themregex2 = ?????; str = str.replace(regex2, ''); What I have...
28 Dec 2015 by aajoyce
I'm a real newbie at regex. I have a banking program and it has regex rules for importing transactions. But sometimes the rules just don't work right. I'm trying to understand just what the rules are saying so that I can make them work the way they should. I think I've figured out some of it...
19 Sep 2012 by Aarti Meswania
if it is Windows Applicationset property of Textbox CharacterCasing = Lowerit will automatically convert uppercase letters to lowercase.string pattern="^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,3})$";and verify it when textbox's Leave event...
18 Feb 2013 by Aarti Meswania
my string is ...var patt1='/:)/g';alert(':):):)'.replace(patt1,"*"));o/p = :):):)I want o/p***How to do?I am newbie in regex...please help to solve this :)
20 Aug 2013 by Aarti Meswania
Hi experts,string Input = "@a = '1', @b = 'hello@123.com,hi@123.com'"I want to split Input string and want output as below using regexary[0] = a = '1'ary[1] = b = 'hello@123.com'I want to split by @ and it should not be in single quotesthanks in adv.
20 Aug 2013 by Aarti Meswania
I got solution after some trial & error,Hope it will useful for people have same requirementstring Input = "@a = '1', @b = 'hello@123.com,hi@123.com'"string Output = Regex.Split(Input, @"(?=@\w+[ =|=])@")(condition)true|false(?=@\w+[ =|=]) condition @ if true then split...
28 Nov 2013 by Aarti Meswania
Hello experts!I want to search criteria usingRegex for find all files which contain word1 but not contain word2in visual studio projectplease help
19 May 2022 by abc174
Hi, I'm working on a define a new language with lex and yacc. However, lexer works fine but yacc doesn't. I'm not pretty sure if my grammar is completely right. I get "syntax error in input" from yacc. My lexing.py: # lexer.input(data). Reset...
28 Jun 2011 by abdugo
I need a Regular Expression to split all SQL query in a file..like that file I need to split all queries from it.. to an array..CREATE TABLE IF NOT EXISTS `collections` ( `collectionid` int(3) unsigned NOT NULL auto_increment, `collectiongroupid` int(2) NOT NULL, `name`...
12 Apr 2016 by abdulsafran
Below is my input and expecting outputInputකtext text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text­ගExpecting OutputThis is my regex find and...
12 Apr 2016 by abdulsafran
I found the solution. Thank youstring replacedString = Regex.Replace(richTextBox1.Text, "ක([^ග]*)ග", @""); richTextBox1.Text = replacedString;
16 Jan 2011 by Abhinav S
Try something like -Regex r = new Rexgex(@\"\\s+\");inputstring = r.Replace(inputstring, " ");
19 May 2011 by Abhinav S
You can use Regex.Replace() to do this.e.g. Regex.Replace(myString, @"\d", string.empty);
15 Apr 2014 by Abhinav S
\s matches any white space characters - http://msdn.microsoft.com/en-us/library/az24scfc%28v=vs.110%29.aspx[^].
19 Mar 2015 by Abhinav S
Try these links -Regular Expression to Validate File Path and Extension[^]How To Use Regular Expression To Validate File Path[^]
27 Mar 2015 by Abhinav S
String.Replace[^] will help you replace string with other text.
20 Jan 2015 by Abhiroop09
Hello,I am new to regular expression. I need to extract between two string pattern ([[tag: & ]]) and for each occurrence.For Example:If the text is :Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque...
19 Dec 2012 by Abhishek Pant
have a look at thisWhy LINQ to SharePoint is a Bad Idea![^]
14 Aug 2010 by Abhishek Sur
\d+/2010Do you want this?Replace the current year in the string with DateTime.Now.Year
29 May 2021 by adideva98
Since I am new to Regex this question might seem silly. I have a huge text file in the Devanagari script (nearly 70k lines). The majority of the file is in Devanagari except for the word tags which are in English alphanumerics. This file has four...
29 Jun 2015 by Aditya_Goud
i am working on a project where i need to make validation to allow only letters and spacesanything except that should return false.. how can i do this ?i have my working codebool usernamecheckchar = txboxName.Text.Any(x => !char.IsLetter(x)); if (usernamecheckchar ==...
30 Jun 2010 by Adrabi Abderrahim
this is by Perl (just example)#!/usr/bin/perl -wuse strict;my $url = 'adrabi';$url=~s/(]+)>)(.*?)()/$1$4/;print "$url\n";my $q = "";$url=~s/$q/You Win/;print $urloutput :
29 Oct 2014 by Adrian Fatol
Try this:$text = 'This is testing \title{string} with \title{test} tags.';preg_match_all('/\\\title\{([\w]+)\}/', $text, $matches);var_dump($matches);
1 Nov 2014 by Afzaal Ahmad Zeeshan
If you know the exact string then you can convert the string to array, and then extract the string at that index. The index of the city is known in the template string. Just extract the string at that index, make sure that the template is followed while typing the command.Something like...
29 Aug 2013 by AG-46677
Objective - To extract the decimal data (length information) from the random incoming string patterns.Sample Incoming string patterns - Pattern 1- some text. Total Pipe length for this project are 1234.5 m. some text...Pattern 2- some text.Total Pipe length for this project 500.0 m some...
9 Jan 2012 by agent154
I'm trying to figure out a regex that will match only a properly formatted string, and return false if any part of it is invalid.My string is in the format of filesystem wildcards separated by a comma to allow for multiple wildcards to be put in one string. I am able to come up with a regex...
31 Oct 2012 by Ahmedwaheed
try this expression^-?\s?[0-9]{1,}(\.[0-9]{2})?$
23 Oct 2019 by Akash Tawade
Input:- ((ab)|(ba)) 2 ((a|b)*) 5 ((a*)(b(a*))) 100 Output:- 2 32 100 here we have Only regex(r) and string length(l) as a parameter in function.....from these two parameter we have to find count of string which is match to our regex. What I have tried: var count = 0; ...
28 Apr 2017 by Akhil Jain
i have made this for 1 decimal place but don't know how to limit upto 6 accepted value 6.0 1.1 0.0 2.1 5.9 not accepted 6.1 6.01 6.2 0.00 5.99 "^[0-9]+(\.[0-9]{1,1})?$" What I have tried: i have made this for 1 decimal place but don't know how to limit upto 6 ^[0-9]+(\.[0-9]{1,1})?$"
3 May 2017 by Akhil Jain
i want a Regular expression for Numbers only with no special character that should accept decimal value with 1 decimal place accepted value 3 3.2 4 5 not accepted value abv '' ! # $ % & *) () 4.55 . 123ert.123 ftr456.34 123tr 3. What I have tried: i want a Regular expression for Numbers...
26 Jun 2012 by akhilgaur1988
I have an HTML page that contains some filenames that i want to download from a webserver. I need to read these filenames in order to create a list that will be passed to my web application that downloads the file from the server. These filenames have some extention.I have digged about this...
8 Sep 2011 by Al Moje
Hi,See this link if it could help.Get URL Parameters Using JavascriptAn easy way to parse the query string in your URL to grab certain values.Regards,Algem
18 Jul 2016 by Ali Sheikhizadeh
Hi , I want to replace String that is like this : [TEST] [QUESTION]A text is between two Square BracketsWhat I have tried:I tried to use Regex with this pattern : [\[][A-Z0-9][\]]But this pattern only get strings like this : [A], [B], [C], [1]
18 Jul 2016 by Ali Sheikhizadeh
Just Used : [\[A-Z0-9\]] And Worked
24 Jun 2013 by Am Gayathri
Hi ,In my project am using one comparevalidator to check the value in textbox is integer or not.
8 May 2014 by Amalraj Ramesh
try this i hope this ill help you.((?=.*\d)(?=.*[A-Z])(?=.*\W).{8,8})
2 Jun 2011 by ambarishtv
use this pattern :)/() ([^)/g
16 Jan 2015 by Ambati Dilip
Hello,I have a string Input: Mynameisblablabla.IstayatblablaNow i need a regular expression in c# where it automatically splits the string intoMy name is blablabla. I stay at blablain single step.
1 Apr 2014 by Ami_Modi
I want to replace all the HTML tags in string with space. What regular expression should I use for that?Input: Hi [FNm] [LNm]:Thanks for downloading.Output: Hi [FNm] [LNm]: Thanks for...
1 Apr 2014 by Ami_Modi
One more answer I got. If anybody referring this question can try this:"]*>"
12 Mar 2017 by Amien90
Regex regex = new Regex(@"a|b");Match match = regex.Match(line); if (match.Success){}I want to know if the line matched on "a" or "b" .. because based on that result i want to do different thingsWhat I have tried:my best solution was to split the regex itself if...
15 Oct 2011 by Amir Mahfoozi
I suggest \d+ (at least one digit)but if you want to capture scientific numbers :[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?test it here :http://www.regular-expressions.info/javascriptexample.html[^]
24 Oct 2011 by Amir Mahfoozi
i. Find a "a" followed by either zero or more times "aa"s should be seenii. Find a "a" followed by either zero or more times of this pattern : (zero or more times "b" followed by zero or more times "ab" followed by zero or more times "ab" )iii. Find a "b" followed by either zero...
16 Jun 2013 by Amit Bezalel
Using the power of regex to parse your logs
26 May 2015 by Amit Samnani
Hi,everyone Question: i have one text-box and i want to allow only numeric value to this text-box like i.e .2,2.3,23.233 Solution: For the above, i already try regex ("^\s*(?=.*[1-9])\d*(?:\.\d{1,4})?\s*$")but still it can`t be allow after point value like...
18 Oct 2014 by amit29391
refer thishttp://stackoverflow.com/questions/2842823/how-to-prevent-white-spaces-in-a-regular-expression-regex-validation
19 Apr 2011 by amit_upadhyay
Iam having a trouble implementing a certain technique - selecting next text in a richtextbox control which is underlined.You see iam trying to make a word processor but am stuck. I have been able to write the code to select each word one-by-one on pressing F1. Now i want to select only those...
12 Nov 2011 by amit_upadhyay
Alright first my situation :1) a text document which has content like :uber|uber|ultrataxi|taxi|taxi cab|cab|minicab|airport taxi|airport transfergenitalia|genitalia|genitalsskincare|skincare|skin care|cosmetic|natual skin care|natural skin...
13 Nov 2011 by amit_upadhyay
Hello, i want to create a regex to be able to replace a string with another string while also making sure that a particular pattern is avoided from being replaced.Eg : string is - I wanted to buy a house and this house is so good that i will {buy|purchase|rent|acquire} this placeThe...
1 May 2013 by amitesh1989
Hi coder i am new in regular expression so i don't know much about i will show you what i have and what i did. String on which i have to put regular expression i have two search ticket number in it like 220-7242648582 for this i use [0-9||-]{14} regexnow the think is that it is a repeated...
3 May 2013 by amitesh1989
Hi I have a list of string in which i have few name in the same line when i use my code i got all name in the same series what i want is to first user in first row of datagrid and second in second. here is my code:using System;using System.Collections.Generic;using...
6 May 2013 by amitesh1989
I have a string in which i have to find PNR number. To get PNR number i have to check two condition (a) First in Passenger should be more than one.(b) Second String contain ITINERARY REBOOKED.if both upper condition exist then only PNR number extract from the string otherwise noexample...
8 May 2013 by amitesh1989
I have a string in which I have to find PNR number. To get PNR number I have to check two condition:First in Passenger should be more than one.Second String contain ITINERARY REBOOKED.If both condition met then extract only the PNR number from the string otherwise return nothing.My...
9 May 2013 by amitesh1989
I have case. I want to select multiple passenger names from the input. if input contains only single passenger name then avoid that input string.I have create regular expression in which It's work for multiple names from the input but it's not work, when I want to avoid single passenger name...
9 May 2013 by amitesh1989
i use this regex (2\.[A-z]\S(.+)) it will fetch multiple pax and avoid single pax
10 May 2013 by amitesh1989
I have a list of name in series like:1.KHOON/FRANCIS 2.MEH/SAY 3.MEH/PRAY 4.MEH/MAW 5.REH/LAW 6.REH/PAE 7.REH/DO 8.REH/LEE 9.REH/HEHnow what i want is to show these name in grid like thisKHOON/FRANCISMEH/SAYMEH/PRAYMEH/MAW and so on i dont want number and . into...
10 May 2013 by amitesh1989
Hi coder i have a question which i won't be able do so may be you guys can help mei have string which contain name in series like thisFXP/R,U 1.WROBEL/TERESA 2.WROBEL/MAREK ...
14 May 2013 by amitesh1989
I have a string in which i want to fetch number before TTL using regex how can i do thatthis is my string WPNCB« 19AUG DEPARTURE DATE-----LAST DAY TO PURCHASE 21MAY/2359 BASE FARE EQUIV AMT TAXES ...
14 May 2013 by amitesh1989
i just made some changes in my regex and it works(?:\b)([0-9]{0,}[.][0-9]{2})(?=TTL)
14 May 2013 by amitesh1989
just made some changes in mt regex and it works(?:\b)([0-9]{0,}[.][0-9]{2})(?=TTL)
24 May 2013 by amitesh1989
I have a large string in which two small string are same but not exactly what i want is to select only one string from that large string not the other stringthis is my stringRP/NYC1S21DD/NYC1S21DD WS/SU 6MAY13/0503Z 29WHVE // i want to select 29WHVE ...
18 Apr 2012 by AmitKumar89
Hi there,Use this JS in your button click.function countWords(){ var txt=$find("TextBox1"); if(txt.value.split(' ').length
26 Aug 2014 by Amol Lendave
Hello friends, I want regular expression for validating input between -100 to 100 onlyInput like 99.99 and -99.99 and 0 tooCurrently i used : ^100$|^\s*(\d{0,2})((\.|\,)(\d{0,2}))?\s*\%?\s*$ Above reg-ex will allow 0 to 100 decimal up to 2 digit.Please Help Me ASAPThanks...
28 Feb 2014 by amshekar
1)How to restrict the user to enter only alphanumerics with $ @ * #using javascript and also it should allow user to click backspace delete and space any help ?2) How to make text box amount field with 3 decimal by defaulti.e if user enters ant digit like 6 it will post 6.000 to...
15 Oct 2012 by Anandkumar D
You need to handle it (style with div) separately........data.Replace("", "").Replace("", String.Empty).Replace("", "").ToString();
27 Aug 2015 by Anant K Sinha
I would like to write pattern for regular expression which will maintain the sequence of word.For Example:I have one string like below: string input="I visited this place with my friends twice. Normal outdoor sitting but yet pleasing. Food is very delicious and sumptuous. However...
14 Feb 2023 by Andre Oosthuizen
Assuming that your text of - Lorem ipsum span sit amet, consectetur adipiscing elit. Sed span velit, vestibulum vel pellentesque sed, mattis et span. Morbi justo est, pharetra vitae nisl at,
15 Oct 2010 by Andreas Gieriet
This regular expression has several flaws:1) looks to my that it has too many groups: (...)2) too many * that lead to an exhautiive search (n-square of the document length?)3) if it is multiline, the . will not match the end-of-line4) there must be some non-greedy repetition, otherwise,...
2 Feb 2012 by Andreas Gieriet
Brute force?\b[Mm][Yy]-[Nn][Aa][Mm][Ee]\bAndi
6 Feb 2012 by Andreas Gieriet
If you want to remove the whole select element with it's content, then you can do it with regex only if some constraints are met: no nested element of the same name (select in this case).Try this:string pattern = @"";CheersAndi
14 Feb 2012 by Andreas Gieriet
A "single-liner" to decode quoted-printable data.
15 Feb 2012 by Andreas Gieriet
I think the following Regex and HtmlDecode would do:string html = ...;string textonly = HttpUtility.HtmlDecode( Regex.Replace(html, @"|", ""));Any HTML construct that would not be stripped off properly by this?
29 Aug 2012 by Andreas Gieriet
So, you allow (I assume with leading and trailing spaces)1. lines of only spaces, zero to any number of them2. plain integral numbers3. numbers with decimal part and optional part before the decimal partThe regex for these individual parts is:1. ^\s*$2. ^\s*\d+\s*$3....
6 Sep 2012 by Andreas Gieriet
Guessing from your question, I unerstand that you have groups of 5 to 6 digits, plus mixed in some single a-z characters (none or one per group).If you have to only take these groups that match your constraints (at most one [a-z] in a group), then I have the following solution:string...
20 Jan 2013 by Andreas Gieriet
How about:...string input = ...;Match match = Regex.Match(input, @"\w+\s*=\s*(\d+(?:\.\d+)?\s*[A-Z]+)");string size = match.Success ? match.Groups[1].Value : string.Empty;...Depends very much on the input string if the @ and { are relevant to properly match the text.Note: If you...
20 Feb 2013 by Andreas Gieriet
To parse source code, you should:treat the whole file as "singleline", i.e. the Regex should not treat new lines as special charactersallow whiespaces between language tokens (all these \s* look a bit ugly, but is necessary to catch all entries)carefully tokenize, e.g. if you only expect...
26 Feb 2013 by Andreas Gieriet
Use the following approach:string input = ...;string pattern = @"^[\s0-9"+Regex.Escape("-+().,")+"]+$";if (Regex.Match(input, pattern).Success) { /* success actions... */ }CheersAndi