|
So here I was, trying to figure out why I was having problems with my ASP RegEx validator control. The objective was to match to an IP address, but to allow it to end in "*" in the final octets (that is, 255.255.255.255 is valid, and so is 255.255.255.* or 255.255.*.* or 255.*.*.*).
But the validation was failing for 0.0.0.200, or anything over 199 in the last octet.
Here is what I had:
ValidationExpression="((1?\d?\d)|(2[0-4]\d)|(25[0-5]))\.(([*]\.[*]\.[*])|(((1?\d?\d)|(2[0-4]\d)|(25[0-5]))\.(([*]\.[*])|(((1?\d?\d)|(2[0-4]\d)|(25[0-5]))\.([*]|((1?\d?\d)|(2[0-4]\d)|(25[0-5])))))))"
Here is the fix:
ValidationExpression="((25[0-5])|(2[0-4]\d)|(1?\d?\d))\.(([*]\.[*]\.[*])|(((25[0-5])|(2[0-4]\d)|(1?\d?\d))\.(([*]\.[*])|(((25[0-5])|(2[0-4]\d)|(1?\d?\d))\.([*]|((25[0-5])|(2[0-4]\d)|(1?\d?\d)))))))"
By reversing the order (priority) of the octet matches, I solved the problem...
|
|
|
|
|
You might consider making it more readable, e.g.:
string b = @"25[0-5]|2[0-4]\d|1?\d?\d";
string n = @"(?:"+b+@")";
string w = @"(?:\*|"+b+@")";
string d = @"\.";
string ip = n+d+w+d+w+d+w;
Cheers
Andi
|
|
|
|
|
Hello,
Why
\\begin\{verbatimtab\}[\n.]*\\end\{verbatimtab\}
does not match
\begin{verbatimtab}
10 56
3 235
\end{verbatimtab}
Any help appreciated,
Greetings - Jacek
|
|
|
|
|
You generally need a modifier tacked on the end of the regex, or an extra parameter in the call to match or whatever to specify multiline input. I'm guessing your current call stops scanning at the first newline.
Sorry I can't be more specific without knowing what regex engine/language/context you're using.
Cheers,
Peter
Software rusts. Simon Stephenson, ca 1994.
|
|
|
|
|
Thanks for answering!
I tried to mess around NL settings, but without success. Could you get it working here: http://www.regexplanet.com/simple/index.html[^]? I'll be fine then (hopefully).
Ultimately, I want to use it in VS find/replace dialog (Notepad++ does not support NL in regex ).
Greetings - Jacek
|
|
|
|
|
I didn't look closely at the middle of your regex. Your fragment [\n.]* matches any number of (newline or fullstop) Inside the square brackets, the fullstop is not a wildcard. That is probably not what you want. A simple .* is probably what you need if you are not interested in capturing the content, given that the multiline flag should take care of "any character" matching newline.
Cheers,
Peter
Software rusts. Simon Stephenson, ca 1994.
|
|
|
|
|
Peter_in_2780 wrote: Inside the square brackets, the fullstop is not a wildcard.
Oh, that was too easy
BUT, it would work if VS allowed to setup regex in a find/replace dialog. It is not possible though. However, the \n works. How to code that regex if . does not catch a new-line and it cannot be changed? (answer: code it yourself, but I would be nice if it wasn't necessary)
Greetings - Jacek
|
|
|
|
|
Huh, it's not very critical thing, but, unfortuantely, begun to like the research.
I have tried just now:
string replaced = Regex.Replace(all, @"\\begin\{verbatimtab\}.*\\end\{verbatimtab\}", (Match m) => (m.Value), RegexOptions.Singleline);
The SingleLine mode specs: "Specifies single-line mode. Changes the meaning of the dot (.) so it matches every character (instead of every character except \n)."
Didn't work.
Greetings - Jacek
|
|
|
|
|
Is this solved?
If not, and if you are talking about C# Regex, then the following will do:
string p = @"\\begin\{verbatimtab\}[\s\S]*?\\end\{verbatimtab\}";
Console.WriteLine("{0}", Regex.Match(str1, p).Success);
The trick:
- use
[\s\S] to match any character, independent of singleline or multiline setting - use lazy match
*? to allow multiple such groups to be matched individually
Cheers
Andi
modified 7-Apr-12 18:59pm.
|
|
|
|
|
The topic is pretty outdated -- I have done it manually for much less time than I had spent trying to build a proper regex
Thanks for a nice trick to ignore multiline settings, though.
Greetings - Jacek
|
|
|
|
|
Hi
I'm using C# and I have the regex below to split words and not split a string ".. .." instead take the whole string as on item to a List.
Example:
text="all "1 dl""
after split
all[0]="all"
all[1]="1 dl"
I found it in Google and it works, but I don't understand how it works.
string regexSpliter = @"(?<=^(?:[^""]*""[^""]*"")*[^""]*) ";
List<string> all =new List<string>_ (System.Text.RegularExpressions.Regex.Split(text, regexSpliter));
And if I remove the space before the last " in the string it doesn't work as I want. It seems like it splits all the characters in to elements written in the string text.
Can anybody please explain the string regexSplitter and why it has to be a space last in the string.
Many thanks
Fia
|
|
|
|
|
From what I can tell, that should be matching a SPACE that follows a anycharactersotherthanaquoteQUOTEanycharactersotherthanaquoteQUOTEanynumberofquotes , but you say it matches your test string so I must be misreading it.
At any rate, you can make something simpler. What exactly do you have and what do you want from it?
|
|
|
|
|
Hi
Thanks for all replies.
I'm trying to get words and if there are strings between quotes get that too that users have entered in a textbox. But I still don't understand how the string regexSplitter works. And I still don't understand why it has to be a space last in that string. Because when I remove it, it doesn't work as I want.
Thanks
Fia
|
|
|
|
|
There has to be something. Have you tried other characters?
|
|
|
|
|
Hi
What do you meen by something? I can write any characters I want in a word or a string. For example the text can contain
'hello by "to much" 10'.
Thanks
Fia
|
|
|
|
|
I mean the SPACE (or something else) needs to be there.
|
|
|
|
|
See my explanation below (I know, this is very old topic, but I see it was not solved in this thread, so I added my lengthly explanation below).
The Regex matches for spaces where the prefix expression ((?<=...) ) matches.
Far too complicated for cases where one wants to have a string split into part separated by spaces, ignoring spaces within "...".
My preferred solution is using positive match criterion (as described in the sentence above):
string pattern = @"\s*(""[^""]*""|\S+)\s*";
var fields = Regex.Matches(input, pattern).Cast<Match>().Select(m=>m.Groups[1].Value);
Cheers
Andi
|
|
|
|
|
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:
..."[^"]*"... : "..."...[^"]*"[^"]*"... : any number of non-"-char, followed by "..." from 1. above...(?:[^"]*"[^"]*")*... : any repetition of the group described in 2. above...^(?:...)*... : 3. above must match from the beginning of the text...^(?:...)*[^"]*... : 4. above, followed by any number of non-"-char(?<=...) : 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*";
string[] split = Regex.Matches(input, pattern).Cast<Match>().Select(m=>m.Groups[1].Value).ToArray();
Cheers
Andi
modified 8-Apr-12 7:03am.
|
|
|
|
|
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:
|
|
|
|
|
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.
|
|
|
|
|
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.
|
|
|
|
|
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.
|
|
|
|
|
^(?=.*[a-zA-Z])(?=.*[0-9])(?=.*[^a-zA-Z0-9]).{6,}$
- ^ checks for the start of the input string.
- (?=.*[a-zA-Z]) checks that there are at least one letter.
- (?=.*[0-9]) checks that there are at least one digit.
- (?=.*[^a-zA-Z0-9]) checks that there are at least one special character (not a letter nor digit).
- .{6,} checks that there are at least 6 characters.
- $ checks for the end if the input string.
See also
|
|
|
|
|
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.
|
|
|
|
|