I'm trying to use a RegularexpressionValidator to match an IP address (with possible wildcards) for an IP filtering system.
I'm using the following Regex:
"([0-9]{1,3}\\.|\\*\\.){3}([0-9]{1,3}|\\*){1}"
Which works fine when running it in LINQPad with Regex.Matches, but doesn't seem to work when I'm using the validator.
Does anyone have a suggestion as to either a better Regex or why it would work in test but not in situ?
Cheers, Ed
This: \\.|\\*\\. looks like the dodgy bit. Do this instead:
#"^(([0-9]{1,3}|\*)\.){3}([0-9]{1,3}|\*)$"
And to only accept 0-255 (thanks, apoorv020):
^((([0-9]{1,2})|(1[0-9]{2,2})|(2[0-4][0-9])|(25[0-5])|\*)\.){3}(([0-9]{1,2})|(1[0-9]{2,2})|(2[0-4][0-9])|(25[0-5])|\*)$
asp:RegularExpressionValidator does not require you to double-escape backslashes. You should try:
([0-9]{1,3}\.|\*\.){3}([0-9]{1,3}|\*){1}
[0-9]{1,3} would allow IP addresses of the form 999.999.999.999 . Your IP address range should allow only 0-255.
Replace all occurences of [0-9]{1,3} with
([0-9]{1,2})|(1[0-9]{2,2})|(2[0-4][0-9])|(25[0-5])
This does seem very complicated to me, and probably there are better ways of doing this, but it seems correct at first glance.
How about putting start and end string characters on the expression
^([0-9]{1,3}\\.|\\*\\.){3}([0-9]{1,3}|\\*){1}$
My answer is general for .NET, not RegularExpressionValidator-specific.
Regex string for IP matching (use ExplicitCapture to avoid useless capturing and keep RE concise):
"\\b0*(2(5[0-5]|[0-4]\\d)|1?\\d{1,2})(\\.0*(2(5[0-5]|[0-4]\\d)|1?\\d{1,2})){3}\\b"
Depending on particular use case you may want to add appropriate anchors, i.e. \A or ^ at the beginning and \Z or $ at the end. Then you can remove word-boundaries requirement: \b.
(Remember about doubling \ inside the string)
Related
I need to check a string that contains a list of e-mails. These emails are usually separated by commas, but I need to check if somewhere in that list there is a delimiter other than a comma. Here's an example:
email1#email.com,email2#email.com,email3#email.com#email4#email.com
I need to identify that different character and replace to a comma.
I cannot just use a regex to identify special characters other than the comma and replace them because emails may have some of these characters. So I need to find something between two e-mail.
I made the following regex to identify an e-mail and I believe it will cover most of the emails:
^[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*#[a-z0-9]+(\.[a-z0-9]+)+$
But I'm a little lost on how to use it to solve my problem, using C #. I need to capture something that was between two matches of this regex and replace to a comma.
Could anyone help me?
Thank you.
Your problem is unsolvable because the delimiter can not always be determined by a human.
Consider this input where the delimiter is a .:
user#server.co.uk.user#otherServer.com
Is this:
user#server.co | uk.user#otherServer.com
or is it:
user#server.co.uk | user#otherServer.com
Or this input:
user#server.intuser#otherServer.com
Is it delimiter u:
user#server.int | ser#otherServer.com
Or delimiter t:
user#server.in | user#otherServer.com
If you're not willing to accept a certain percentage of failures, you're better off looking for ways not to receive this input to begin with.
([^#,]+#[^.]+\.\w{3}(?!,|$)).
Try this.Replace by $1,.See demo.
http://regex101.com/r/tF4jD3/15
P.S this will work for email id's of format something#something.com.
I can't think of an elegant way to achieve this. If you don't mind an inelegant solution, you can replace any top level domain plus one character with the same TLD plus comma.
You'll end up replacing ".com#" with ".com,", ".eu*" with ".eu," and so on. Replacement could take place using Regex so your iterations will be the same number of the TLDs you want to replace.
One option you could try is to split the incoming string using the # symbol and check that each part of the resulting array has a comma in int--except the first and last.
If you find one that is missing the comma do a search for the .com or .net or .org in that element and stick a comma after that character.
Lastly just run splice the list back together with the # symbol
Thanks for the replies.
The string must have only commas as the delimiter.
The example I mentioned was just to illustrate, because this list was generated using a jquery plugin that had a flaw that was noticed only after allowing it to be saved in the list something like "email1#email.comemail2#email.com" or any other combination non standard "email1#email.com,email2#email.com".
My main concern is cases like "email1#email.com/email2#email.com"
I'm trying to automate a search for this kind of inconsistency, as prevention.
I thought about using regex but I really do not know if it is the best approach.
I am now thinking, as it is not a critical part of the system, it would be a simpler way just to use a list of invalid characters to make the replace.
But I will try the vks's solution.
Thank you all.
Live demo: http://regex101.com/r/wW6wC4
I'm trying to add a regex expression that allows email addresses like:
asdf.asdf#test-dom-a.com
([\w+\.]+#[\w]{1,})(\.)([0-9a-zA-Z\.\-]{1,})
^---- Thought this would allow hyphens...
what am I missing here?
Your pattern requires that the hyphen appears after a period. Try this instead:
([\w+.]+#[\w-]{1,})(\.)([0-9a-zA-Z.-]+)
Demonstration
Or more simply:
([\w+.]+#[\w.-]+)
Although the second pattern doesn't require that the second part of the address contains a period.
Demonstration
Your hyphen code appears in the segment that checks characters after the first period in the domain name. You need to add it to the match block before the domain name:
([\w+\.]+#[\w\-]{1,})(\.)([0-9a-zA-Z\.\-]{1,})
^^---- check here as well.
In reality, I would search for a more comprehensive email regex - the one you have doesn't seem robust enough IMHO.
Your regex:
([\w+\.]+#[\w]{1,})(\.)([0-9a-zA-Z\.\-]{1,})
This will allow hyphen as last character only.
To allow it anywhere use:
^([\w+.-]+#[\w-])(\.)([0-9a-zA-Z.-])$
OR to allow it only in between use (except first and last position):
^[\w+.-]*#\w[\w-]*\.[\w-]*[0-9a-zA-Z.]+$
Working Demo: http://regex101.com/r/lQ1nV7
You're not matching strings of the form "asd#fge.hj-kl", which as you can see not what you want.
([\w+\.]+)#([0-9a-zA-Z\.\-]{1,})\.com
([\w+\.]+)#([0-9a-zA-Z\.\-]{1,})\.([\w]{1,})
I'm currently facing a (little) blocking issue. I'd like to replace a substring by one another using regular expression. But here is the trick : I suck at regex.
Regex.Replace(contenu, "Request.ServerVariables("*"))",
"ServerVariables('test')");
Basically I'd like to replace whatever is between the " by "test". I tried ".{*}" as a pattern but it doesn't work.
Could you give me some tips, I'd appreciate it!
There are several issues you need to take care of.
You are using special characters in your regex (., parens, quotes) -- you need to escape these with a slash. And you need to escape the slashes with another slash as well because we 're in a C# string literal, unless you prefix the string with # in which case the escaping rules are different.
The expression to match "any number of whatever characters" is .*. In this case, you would want to match any number of non-quote characters, which is [^"]*.
In contrast to (1) above, the replacement string is not a regular expression so you don't want any slashes there.
You need to store the return value of the replace somewhere.
The end result is
var result = Regex.Replace(contenu,
#"Request\.ServerVariables\(""[^""]*""\)",
"Request.ServerVariables('test')");
Based purely on my knowledge of regex (and not how they are done in C#), the pattern you want is probably:
"[^"]*"
ie - match a " then match everything that's not a " then match another "
You may need to escape the double-quotes to make your regex-parser actually match on them... that's what I don't know about C#
Try to avoid where you can the '.*' in regex, you can usually find what you want to get by avoiding other characters, for example [^"]+ not quoted, or ([^)]+) not in parenthesis. So you may just want "([^"]+)" which should give you the whole thing in [0], then in [1] you'll find 'test'.
You could also just replace '"' with '' I think.
Taryn Easts regex includes the *. You should remove it, if it is just a placeholder for any value:
"[^"]"
BTW: You can test this regex with this cool editor: http://rubular.com/r/1MMtJNF3kM
I need a regex for the following criteria:
Atleast 7 alphanumeric characters with 1 special character
I used this:
^.*(?=.{7,})(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[##$!%^&+=]).*$
It works fine if I type Password1! but doesnt work for PASSWORD1!.
Wont work for: Stmaryshsp1tal!
I am using the Jquery validation plugin where I specify the regex.
When I use a regular expression validator and specify the following regex:
^.*(?=.{7,})(?=(.*\W){1,}).*$
It works perfectly without any issues. When I set this regex in the Jquery validation I am using it doesnt work.
Please can someone shed some light on this? I want to understand why my first regex doesnt work.
(?=.\d)(?=.[a-z])
tries to match a digit and an alphanumeric character at the same place. Remember that (?= ... ) does not glob anything.
What you want is probably:
^(?=.*\W)(?=(.*\w){7})
This is exactly the same as veryfying that your string both matches ^.*\W (at least one special character) and ^(.*\w){7}) (7 alphanumeric characters. Note that it also matches if there are more.
Try this regex:
\S*[##$!%^&+=]+\S*(?<=\S{7,})
EDIT3: Ok, this is last edit ;).
This will match also other special characters. So if you wan't limit the number of valid characters change \S to range of all valid characters.
Here is the regex , I think it can handle all possible combination..
^(?=.{7,})\w*[.##$!%^&+=]+(\w*[.##$!%^&+=]*)*$
here is the link for this regex, http://regexr.com?2tuh5
As a good tool for quickly testing regular expressions I'd suggest http://regexpal.com/ (no relations ;) ). Sometimes simplifying your expression helps a lot.
Then you might want to try something like ^[a-zA-Z0-9##$!%^&+=]{7,}$
Update 2 now including digits
^.*(?=.{7,})(?=.*\d)(?=.*[a-zA-Z])(?=.*[##$%^&+=!]).*$
This matches:
Stmarysh3sptal!, password1!, PASSWORD1P!!!!!!##^^ASSWORD1, 122ss121a212!!
... but not:
Password1, PASSWORD1PASSWORD1, PASSWORD!, Password!, 1221121212!! etc
The reason it matches Password1! but not PASSWORD1! is this clause:
(?=.*[a-z])
That requires at least one lowercase letter in the password. The pattern says that the password must be at least 7 characters long, and contain both uppercase and lowercase letters, at least one number, and at least one of ##$!%^&+=. PASSWORD1! fails because there are no lowercase letters in it.
The second pattern accepts PASSWORD1! because it's a far, far weaker password requirement. All it requires is that the password is 7+ characters and has at least one special character in it (other than _). The {1,} is unnecessary, by the way.
If I were you, I'd avoid weakening the password and just leave it as it is. If I wanted to allow all-lowercase or all-uppercase passwords for some reason, I'd simply change it to
^(?=.*\d)(?=.*[a-zA-Z])(?=.*[##$!%^&+=]).{7,}$
...thus not weakening the password requirements any more than I had to.
Basically, the input field is just a string. People input their phone number in various formats. I need a regular expression to find and convert those numbers into links.
Input examples:
(201) 555-1212
(201)555-1212
201-555-1212
555-1212
Here's what I want:
(201) 555-1212 - Notice the space is gone
(201)555-1212
201-555-1212
555-1212
I know it should be more robust than just removing spaces, but it is for an internal web site that my employees will be accessing from their iPhone. So, I'm willing to "just get it working."
Here's what I have so far in C# (which should show you how little I know about regular expressions):
strchk = Regex.Replace(strchk, #"\b([\d{3}\-\d{4}|\d{3}\-\d{3}\-\d{4}|\(\d{3}\)\d{3}\-\d{4}])\b", "<a href='tel:$&'>$&</a>", RegexOptions.IgnoreCase);
Can anyone help me by fixing this or suggesting a better way to do this?
EDIT:
Thanks everyone. Here's what I've got so far:
strchk = Regex.Replace(strchk, #"\b(\d{3}[-\.\s]\d{3}[-\.\s]\d{4}|\(\d{3}\)\s*\d{3}[-\.\s]\d{4}|\d{3}[-\.\s]\d{4})\b", "<a href='tel:$1'>$1</a>", RegexOptions.IgnoreCase);
It is picking up just about everything EXCEPT those with (nnn) area codes, with or without spaces between it and the 7 digit number. It does pick up the 7 digit number and link it that way. However, if the area code is specified it doesn't get matched. Any idea what I'm doing wrong?
Second Edit:
Got it working now. All I did was remove the \b from the start of the string.
Remove the [] and add \s* (zero or more whitespace characters) around each \-.
Also, you don't need to escape the -. (You can take out the \ from \-)
Explanation: [abcA-Z] is a character group, which matches a, b, c, or any character between A and Z.
It's not what you're trying to do.
Edits
In response to your updated regex:
Change [-\.\s] to [-\.\s]+ to match one or more of any of those characters (eg, a - with spaces around it)
The problem is that \b doesn't match the boundary between a space and a (.
Afaik, no phone enters the other characters, so why not replace [^0-9] with '' ?
Here's a regex I wrote for finding phone numbers:
(\+?\d[-\.\s]?)?(\(\d{3}\)\s?|\d{3}[-\.\s]?)\d{3}[-\.\s]?\d{4}
It's pretty flexible... allows a variety of formats.
Then, instead of killing yourself trying to replace it w/out spaces using a bunch of back references, instead pass the match to a function and just strip the spaces as you wanted.
C#/.net should have a method that allows a function as the replace argument...
Edit: They call it a `MatchEvaluator. That example uses a delegate, but I'm pretty sure you could use the slightly less verbose
(m) => m.Value.Replace(' ', '')
or something. working from memory here.