Click here to Skip to main content

Regular Expressions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page  Show 
AnswerRe: Can anybody explain how this Regex works. [modified]memberAndreas Gieriet7 Apr '12 - 12:06 
See The 30 Minute Regex Tutorial and search for all occurances of (?<= in that article. This explains the meaning of (?<=...).
 
You have always to separate the way you enter a pattern in C# and the pattern the Regex sees:
C# @"..." pattern:@"(?<=^(?:[^""]*""[^""]*"")*[^""]*) "
effective Regex pattern (here delimited by /.../):/(?<=^(?:[^"]*"[^"]*")*[^"]*) /
 
I'm now only talking in Regex domain (the 2nd row), not how it is entered in the C# string.
 
Let's start with the inner most part and work outwards:
 
  1. ..."[^"]*"...: "..."
  2. ...[^"]*"[^"]*"...: any number of non-"-char, followed by "..." from 1. above
  3. ...(?:[^"]*"[^"]*")*...: any repetition of the group described in 2. above
  4. ...^(?:...)*...: 3. above must match from the beginning of the text
  5. ...^(?:...)*[^"]*...: 4. above, followed by any number of non-"-char
  6. (?<=...) : match a space that is preceeded by the expression from 5. above; the (?<=...) is not part of the match
 
The Regex searches for the space character and checks if the data before that space matches the prefix expression. If yes, the match is successful, otherwise, the Regex searches for the next space and checks again, etc.
 
The given Regex and the given data match only on one space, the one after all. The underlined part matches with all: (?<=^(?:[^"]*"[^"]*")*[^"]*) .
 
I.e. the regex splits the given data by spaces, respecting spaces within "..." strings as non-separators.
 
Very complicated, though. I would do this differently, namely in positive terms (what you want to be part of the fields rather than what splits them):
 
string pattern = @"\s*(""[^""]*""|\S+)\s*"; // maybe a more sophisticated pattern is
                                            // needed since the above expression seems
                                            // to match more,
                                            // but this is maybe an undesired side
                                            // effect of the complicated expression
string[] split = Regex.Matches(input, pattern).Cast<Match>().Select(m=>m.Groups[1].Value).ToArray();
 
Cheers
Andi

modified 8 Apr '12 - 7:03.

QuestionUrl rewriting rulememberGaurav.goel20913 Sep '11 - 0:18 
Hello there,
 
I need some clever regular expression writing in IIS7 to redirect traffic from one site to another, the pages are similar in structure but information needs re-writing.

Here's an example:

http://www.mysite.com/manufacturers/Name_of_Manufacturer_6828/Name_OF_Product_43146.htm

needs to be structured as the following

http://www.newsite.co.in/free-msds-download/name-of-manufacturer/name-of-product/

Here's what needs changing:

1). Only Alpha, Numeric and hyphen's allowed.
2). Change '_' to '-'.
3). Remove the '_NUMBER' directly after the manufacturer name and before the '/'.
4). Remove the '_NUMBER' directly after the product name and before the '.htm'.
5). Remove the '.htm' and replace with a '/'.

It needs writing into the code below please as a permanent 301 redirect:
AnswerRe: Url rewriting rulememberShahriar Iqbal Chowdhury24 Sep '11 - 11:00 
Hi,
 
You can use the JavaScript below,
var str="http://www.mysite.com/manufacturers/Name_of_Manufacturer_6828/Name_OF_Product_43146.htm";
str=str.replace("http://www.mysite.com/manufacturers/", "");
var key=str.split("/");
var manufacturer=key[0];
var product=key[1];
var to_index=manufacturer.lastIndexOf("_", manufacturer.length);
manufacturer=manufacturer.substring(0, to_index);
manufacturer=manufacturer.replace(/_/gi, "-");
 
product=product.replace(/.htm/gi, "");
to_index=product.lastIndexOf("_", product.length);
product=product.substring(0, to_index);
product=product.replace(/_/gi, "-");
var cleanUrl="http://www.newsite.co.in/free-msds-download/"+manufacturer+"/"+product+"/";
document.write(cleanUrl);
variable cleanUrl holds the url you are looking for.Hope this will help.
AnswerRe: Url rewriting rulemvpAspDotNetDev24 Sep '11 - 11:37 
I am going to make a few assumptions. Since you said this is in IIS, you probably can't create .Net or JavaScript code to handle the redirect (i.e., all redirects must be done using some regexes in a configuration file). Also, I assume the number of underscores in the "mysite.com" URL is variable.
 
I don't think you can do this with one redirect, but you can create several. For example, you can start off with these (create more to handle more underscores):
^http://www\.mysite\.com/manufacturers/(?<MAN>[a-z0-9]+)_[0-9]+/(?<PRO>[a-z9-9]+)_[0-9]+\.htm$
http://www\.newsite\.co\.in/free-msds-download/${MAN}/${PRO}/
 
^http://www\.mysite\.com/manufacturers/(?<MAN1>[a-z0-9]+)_(?<MAN2>[a-z0-9]+)_[0-9]+/(?<PRO>[a-z0-9]+)_[0-9]+\.htm$
http://www\.newsite\.co\.in/free-msds-download/${MAN1}-${MAN2}/${PRO}/
 
^http://www\.mysite\.com/manufacturers/(?<MAN>[a-z0-9]+)_[0-9]+/(?<PRO1>[a-z9-9]+)_(?<PRO2>[a-z9-9]+)_[0-9]+\.htm$
http://www\.newsite\.co\.in/free-msds-download/${MAN}/${PRO1}-${PRO2}/
 
^http://www\.mysite\.com/manufacturers/(?<MAN1>[a-z0-9]+)_(?<MAN1>[a-z0-9]+)_[0-9]+/(?<PRO1>[a-z9-9]+)_(?<PRO2>[a-z9-9]+)_[0-9]+\.htm$
http://www\.newsite\.co\.in/free-msds-download/${MAN1}-${MAN2}/${PRO1}-${PRO2}/
 
That is simple enough, but that could end up being a lot of rules if you have a large range of underscores (e.g., "the_name_of_some_long_product" and "the_name_of_some_long_manufacturer"). Alternatively, you can redirect all of the "mysite" URL's that match the pattern you want to a single URL on "newsite" that takes the original URL as a query string. The page on newsite can then parse the query string and perform a redirect using C# or VB.Net code, which gives you more flexibility than a configuration file.
 
Note: the above regexes are untested and may contain mistakes, but you get the idea.
Somebody in an online forum wrote:
INTJs never really joke. They make a point. The joke is just a gift wrapper.

QuestionRegular expression to check special charactermembersri_34648 Sep '11 - 11:16 
I need to check whether password has at least 6 characters with at least 1 letter and one special character. I am checking the condition !Regex.IsMatch(strPassword,"(?!^[0-9]*$)(?!^[a-zA-Z]*$)^([a-zA-Z0-9]{6,24})$"). But it doesnot allow special character.

I need to change the expression to check if there is at least one special character.

Please help me out.
AnswerRe: Regular expression to check special charactermemberMizard X25 Sep '11 - 0:06 
^(?=.*[a-zA-Z])(?=.*[0-9])(?=.*[^a-zA-Z0-9]).{6,}$
  1. ^ checks for the start of the input string.
  2. (?=.*[a-zA-Z]) checks that there are at least one letter.
  3. (?=.*[0-9]) checks that there are at least one digit.
  4. (?=.*[^a-zA-Z0-9]) checks that there are at least one special character (not a letter nor digit).
  5. .{6,} checks that there are at least 6 characters.
  6. $ checks for the end if the input string.
 
See also

-- MizardX (so)

QuestionHelp with patternmemberPhrone7 Sep '11 - 20:25 
I need help with regular expression for a javascript I'm working on.
I have some html code into a variable that is named xmlDoc. The string includes a table and there are some things there that I want to pick out with regex.
<tr class="myGreyRow">
<td>6</td>
<td id="fn1">Prentice, Bob</td>
<td>340584305858</td>
<td>Logged out</td>
<td>
<a class="listlink" href="javascript:doEditAgent("56746")">Edit</a>, 
							<a class="listlink" href="javascript:doRemoveAgent("56746", "fn1")">Delete</a>, 
							<a class="listlink" href="javascript:doLoginLogout("4", "56746","fn1")">Log in</a>
</td>
</tr>
In this code I want to pick out:
1. The name, "Prentice, Bob"
2. The status, "Logged out"
3. The content from doLoginLogout(), "4", "56746","fn1"
 
What pattern(s) should I use?
There are five rows similar to this in the table that I need to do this on.
AnswerRe: Help with patternmvpthatraja28 Nov '11 - 19:10 
Probably you need to take a look at this[^]
 
But to pick the name, status values, you may try document.getElementById, Look at this Get Value of a cell of HTML Table[^]
thatraja

My Dad had a Heart Attack on this day so don't...
Pompeyboy3 here
| Nobody remains a virgin, Life screws everyone Sigh | :sigh:

QuestionWhat is the Regular expression that detects a $ sign and digits in html codememberMember 82168286 Sep '11 - 7:49 
Hi Everyone,

I am trying to figure out how to parse html line and take from it the $ sign and a the monetary value.

For example:

The price of the book is $15.00 and the price of the computer is $1,299.99 cents.

Thank you.
AnswerRe: What is the Regular expression that detects a $ sign and digits in html code [modified]memberTimothy CIAN6 Sep '11 - 10:54 
\$(\d{1,3}(,\d{3})*)(\.\d\d)?

modified on Wednesday, September 7, 2011 10:24 AM

GeneralRe: What is the Regular expression that detects a $ sign and digits in html codememberMember 82168287 Sep '11 - 5:20 
Thanks for the reply. I doesn't seem to work.
 
Can you explain briefly what you are attempting to match with that expression?
 
Thank you.
GeneralRe: What is the Regular expression that detects a $ sign and digits in html codememberTimothy CIAN7 Sep '11 - 5:27 
It does work. Try this in a console app:
 
static void Main(string[] args)
{
    Regex rx = new Regex(@"\$(\d{1,3}(,\d{3})*)(\.\d\d)?");
    foreach (Match m in rx.Matches("This matches $4.12 dollars.  It also matches $32.32 or $15 or $2.11 or $0.12 or $156,789.33 or $12,345.67"))
    {
        Console.WriteLine(m.Value);
    }
    Console.ReadKey();
}
 
Output:
 
$4.12
$32.32
$15
$2.11
$0.12
$156,789.33
$12,345.67
 
Of course, you must reference the System.Text.RegularExpressions namespace. Also, here is a page you can use to help learn regular expression syntax:
 
http://www.regular-expressions.info/reference.html
GeneralRe: What is the Regular expression that detects a $ sign and digits in html codememberMember 82168287 Sep '11 - 8:48 
Thank you for the sample and the link. It works.
GeneralRe: What is the Regular expression that detects a $ sign and digits in html codeprotectorPete O'Hanlon7 Sep '11 - 5:38 
I ran that Regex against your test case and it works perfectly. What output are you getting? What does your code look like that's calling it?

Forgive your enemies - it messes with their heads

My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility


AnswerRe: What is the Regular expression that detects a $ sign and digits in html codememberjschell7 Sep '11 - 8:32 
Member 8216828 wrote:
I am trying to figure out how to parse html line and take from it the $ sign and a the monetary value.

 
In general it isn't a good idea to parse html/xml via regular expressions.
 
Instead one should use a html/xml parser.
 
One might use regex if all of the following is true.
1. The source(s) for html/xml are limited. Thus for example there is only one source and there is unlikely to be another.
2. The source is machine generated (thus a library was used to create it rather than an adhoc code of some sort.)
 
The reason this isn't a good idea is because there are many variations in the way html/xml can be formatted. By the time one has accounted for all of those variances either one has a parser or one has a maintenance problem (or maintenance nightmare).
QuestionRegular ExpressionmemberMorgSim11 Jul '11 - 22:07 
Hi Guys,
I have three Regex and would like to know if it's possible to combine them into one.
I'm validating phone numbers in the form:
- 0219081234
- 021 908 1234
- 021-908-1234

here are my regex for the above in order:
- var phoneRegex1 = /^(\d{3})(\d{3})(\d{4})$/;
- var phoneRegex2 = /^(\d{3})(\-)(\d{3})(\-)(\d{4})$/;
- var phoneRegex3 = /^(\d{3})(\ )(\d{3})(\ )(\d{4})$/;

Thanks in Advance,
Morgs
AnswerRe: Regular ExpressionmemberFiro Atrum Ventus11 Jul '11 - 22:31 
Just use ? operator
Like this:
^\d{3}-?\s?\d{3}-?\s?\d{4}$
You can flame me whichever way you want and I wouldn't care a bit.
But if you group me with some idiots, I'll turn into your worst nightmare.

GeneralRe: Regular ExpressionmemberMorgSim11 Jul '11 - 23:09 
Thanks a ton Firo,
will give it a try...
 
Morgs
GeneralRe: Regular ExpressionmemberPIEBALDconsult17 Jul '11 - 5:55 
I think that would match values like:
 
123456-7890
123456 7890
123456- 7890
 
123-4567890
123-456 7890
123-456- 7890
 
123 4567890
123 456-7890
123 456- 7890
 
123- 4567890
123- 456-7890
123- 456 7890
123- 456- 7890
 
Which are not in his spec.
AnswerRe: Regular Expression [modified]memberPIEBALDconsult17 Jul '11 - 7:02 
If you want to ensure that the separators (if any) between the groups are the same, I think you need to use a backreference[^].
 
Something like this perhaps:
 
(?'Area'\d{3})(?(?=(\s|-))(?'Sep'(\s|-)))(?'Prefix'\d{3})(?(Sep)\k<Sep>)(?'Number'\d{4})

modified on Sunday, July 17, 2011 2:00 PM

AnswerRe: Regular ExpressionmemberJimmyRopes13 Aug '11 - 11:57 
I think this is what you need.
 
^\d{3}(-|\s)?\d{3}(-|\s)?\d{4}$


Simply Elegant Designs JimmyRopes Designs
Think inside the box! ProActive Secure Systems

I'm on-line therefore I am.
JimmyRopes


AnswerRe: Regular Expression [modified]memberShahan Ayyub23 Aug '11 - 11:00 
Have a look at this pattern:
 
^\d{3}[\s-]?\d{3}[\s-]?\d{4}$

modified on Tuesday, August 23, 2011 8:35 PM

QuestionValue between brackets with a sperator character [modified]membernbgangsta26 May '11 - 2:27 
I'm looking for a regex which can match the a value between brackets and deal with a seperator character within.
This is the format I'm after:
[?#?]
where ? can be one or more characters and # is '+', '-' or '='
 
It should match the following.
[1+1]
[A=T]
[AB=#1#2#3]
 
So far I've got this:
\[.*[\+-=]?\]
But it doesn't work well, it includes text between the 'tags'.
[A=B]ShouldNotInclude[1=3] (but it does )
I'm obviously doing something wrong,the whole regex probably.
Anyone got an idea?
 
Found the problem, not the solution.
Problem is that it jumps to the last ']' character it can detect.

modified on Thursday, May 26, 2011 9:15 AM

AnswerRe: Value between brackets with a sperator charactermembernbgangsta26 May '11 - 3:28 
Got it:
\[(.*?)[\+-=](.*?)\]
AnswerRe: Value between brackets with a sperator charactermemberPeter_in_278026 May '11 - 14:13 
What you saw is called 'greedy' matching. The better regex engines support non-greedy matching operators, which behave a bit more intuitively, even if they make the regex unreadable!
 
Cheers,
Peter
Software rusts. Simon Stephenson, ca 1994.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   


Advertise | Privacy | Mobile
Web03 | 2.6.130523.1 | Last Updated 1 Mar 2013
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid