Asp.net validation expression for a string - asp.net

I want to create a regular expression for subdomain like:
In the textbox user need to enter like
abc1.test.com
or
abc1s.test.com
Note: .test.com is always required at the end.
The variable part can contain any letter, alphabets etc
I dont know anything about regex so i ask this silly question. I googled it for more than 2 hours but dont find any good example.
Please note this is not a homework.
Any help is highly appreciated.

There are a number of useful resources on regex online, including:
http://regexpal.com/ - test out your regex
http://www.regular-expressions.info/reference.html - regex reference
http://www.addedbytes.com/cheat-sheets/regular-expressions-cheat-sheet/ - useful cheat sheet
I am sure other members will have better resources but these have sufficed for me in the past.
The following regex should work for you:
^((.+)(.test.com){1})$
the '.' is any character except new line
the '+' is one or more times
the '{1}' is exactly once

^[\d\w]+\.test\.com$ will work for your case.

Related

Base search/find functionality (Ctrl+F) in AX 2009/2012 doesn't work properly...how to fix?

I feel like I might be losing my mind...but if you search the AOT for anything with double colons "::", it fails completely. I'm trying to step through the Forms\SysAotFind to figure this out but I didn't want to spin my wheels a bunch for something that might be on my system only.
To reproduce in AX 2009, select Classes\SalesTableType, press Ctrl+F and put "CustLedgerAccounts::sumAccount" in the containing text box and click find now. You can see this is clearly located in the Classes\SalesTableType\accountCust method. I've tried searching for base enums inside objects with no luck either.
I noticed the same behavior, but escaping the colons with a backslash makes the search work correctly.
So in your case you would need to search for "CustLedgerAccounts\:\:sumAccount".
The search uses regular expressions in the syntax defined by the match function.
Colon is a special character, hence it needs to be escaped by a backslash.
For those searching for a fix, you can see where the issue is here and just tweak it if you want to allow specifically for double colons:
[c] \Classes\SysTreeNodeSearch\isNodeInRange #46
if (!match(containingText,source))
return false;

asp.net allow german characters in Url

I am using RegularExpressionValidator control with
[http(s)?://]*([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?
regular expression to validate Url. I need to allow german characters
(ä,Ä,É,é,ö,Ö,ü,Ü,ß)
in Url. What should be exact regular expression to allow these characters?
I hope you are aware that it is not easy to use regex for URL validation, because there are many valid variations of URLs. See for example this question.
First your regex has several flaws (this is only after a quick check, maybe not complete)
See here for online check on Regexr
It does not match
http://RegExr.com?2rjl6]
Why do you allow only \w and - after the first dot?
but it does match
hhhhhhppth??????ht://stackoverflow.com
You define a character group at the beginning [http(s)?://] what means match any of the characters inside (You probaly want (?:http(s)?://) and ? after wards instead of *.
To answer your question:
Create a character group with those letters and put it where you want to allow it.
[äÄÉéöÖüÜß]
Use it like this
(?:https?://)?([äÄÉéöÖüÜß\w-]+\.)+[äÄÉéöÖüÜß\w-]+(/[-äÄÉéöÖüÜß\w ./?%&=]*)?
Other hints
The - inside of a character group has to be at the start or the end or needs to be escaped.
(s)? is s?

What's the best way to constraint validation to not allow any spaces in ASP.NET

I have different cases:
No spaces allowed at all
No spaces allowed at the beginning or the end of the string only
..A little question, is it good to check (to validate) the input for spaces through the RegEx of the RegularExpressionValidator ?
The \S escape sequence matches anything that isn't a whitespace character.
Thus the regexes that you need follow:
^\S+$
^\S.*\S$
In your previous question, you mentioned you wanted from 0 to 50 characters. If that's still the case, here's what you want:
/^\S{0,50}$/
/^(?!\s).{0,50}(?<!\s)$/
As of right now, I think these are the only regexes posted that allow for less than one letter with the first pattern, and less than two letters with the second pattern.
Regexes are not a "bad" thing, they're just a specialized tool that isn't suited for every task. If you're trying to validate input in ASP.NET, I would definitely use a RegularExpressionValidator for this particular pattern, because otherwise you'll have to waste your time writing a CustomValidator for a pretty meager performance boost. See my answer to this other question for a little guidance on when and when not to use regex.
In this case, the reason I'd use a regex validator has less to do with the pattern itself and more to do with ASP.NET. A RegularExpressionValidator can just be dragged and dropped into your ASPX code, and all you'd have to write would be 10-21 characters of regex. With a CustomValidator, you'd have to write custom validation functions, both in the codebehind and the JavaScript. You might squeeze a little more performance out of it, but think about when validation comes into play: only once per postback. The performance difference is going to be less than a millisecond. It's simply not worth your time as a developer -- to you or your employer. Remember: Hardware is cheap, programmers are expensive, and premature optimization is the root of all evil.
You know the saying about regex's (now you have two problems) - this is doable without regex and should be more performant and easier to read.
string checkString = /* whatever */
if(checkString.IndexOf(" ") > -1)
// Failed Condition 1
if(checkString.Trim() != checkString)
// Failed Condition 2
No spaces allowed at all:
^\S+$
No spaces allowed at the beginning or end:
^\S+.*\S+$
The System.String class contains everything you need:
No spaces allowed at all
This will handle the case of spaces only:
bool valid = !str.Contains(" ");
If you need to check for tabs as well:
char[] naughty = " \t".ToCharArray();
bool fail = (str.IndexOfAny(naughty) == -1);
There are other whitespace characters you could check for, see Character Escapes for more details.
No spaces allowed at the beginning or the end of the string only
A bit simpler, since Trim() will remove any kind of whitespace, including newlines:
bool valid = str.Length == str.Trim().Length;

Help with a regular expression to validate a series of n email addresses seperated by semicolons

I'm using an asp.net Web Forms RegularExpressionValidator Control to validate a text field to ensure it contains a series of email addresses separated by semicolons.
What is the proper regex for this task?
I think this one will work:
^([A-Za-z0-9._%+-]+#[A-Za-z0-9.-]+\.[A-Za-z]{2,4}(;|$))+
Breakdown:
[A-Za-z0-9._%+-]+#[A-Za-z0-9.-]+\.[A-Za-z]{2,4} : valid email (from http://www.regular-expressions.info/)
(;|$) : either semicolon or end of string
(...)+ : repeat all one or more times
Make sure you are using case-insensitive matching. Also, this pattern does not allow whitespace between emails or at the start or end of the string.
The 'proper' (aka RFC2822) regex is too complicated. Try something like (\S+#[a-zA-Z0-9-.]+(\s*;\s*|\s*\Z))+
Not perfect but should be there 90% (haven't tried it, so it might need some alteration)
Note: Not too sure about \Z it might be a Perl only thing. Try $ as well if it doesn't work.

How to write regex to extract FlickR Image ID From URL?

I'm looking to do do two things, and I am looking to do them in a beautiful way. I am working on a project that allows users to upload flickr photos by simply entering their flickr image URL. Ex: http://www.flickr.com/photos/xdjio/226228060/
I need to:
make sure it is a URL that matches the following format: http://www.flickr.com/photos/[0]/[1]/
extract the following part: http://www.flickr.com/photos/xdjio/[0]/
Now I could very easily write some string methods to do the above but I think it would be messy and love learning how to do these things in regex. Although not being a regex ninja I am currently unable to do the above.
Given an input string with a URL like the one you provided, this will extract the image ID for any arbitrary user:
string input = "http://www.flickr.com/photos/xdjio/226228060/";
Match match = Regex.Match(input, "photos/[^/]+/(?<img>[0-9]+)", RegexOptions.IgnoreCase | RegexOptions.SingleLine);
if(match.Success)
{
string imageID = match.Groups["img"].Value;
}
Breaking it down, we are searching for "photos/" followed by one or more characters that is not a '/', followed by a /, followed by one or more characters that are numbers. We also put the numbers segment into a named group called "img".
thought i would add to this that when using the javascript asp.net validator it doesn't support the grouping name.
the regex to use in this situation would be:
photos/[^/]+/([0-9]+)
thought someone might find this useful

Resources