Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I have this regex example. I'm new to regex.
^[a-z0-9](\.?[a-z0-9_-]){0,}#[a-z0-9-]+\.([a-z]{1,6}\.)?[a-z]{2,6}$
I'm thinking where is the top level domain here, is it starting on the right or on the left? And what is it?
The regex you've provided won't allow Numbers in top level domains.
[a-z]{2,6}$ is the part checking the top level domain. It'll only allow lower case characters, minimal 2 characters max 6 characters.
EDIT: Let's deconstrunct your your regex for clearity.
^[a-z0-9](\.?[a-z0-9_-]){0,}#[a-z0-9-]+\.([a-z]{1,6}\.)?[a-z]{2,6}$
the ^[a-z0-9](\.?[a-z0-9_-]){0,} part checks from the beginning of the string if there are only allowed characters that are a-zand 0-9 followed by an optional . followed by a-zand 0-9 aswell as _ and - zero to infinite times.
#[a-z0-9-]+\.([a-z]{1,6}\.)? checks for an # after the first part aswell as validity of the domain, a-z and 0-9 including - followed by a . with an optional a-z2 to 6 times, up to one time i.E. google.com would be acceptable at this point but the .com part is optional.
[a-z]{2,6}$ checks for a-z 2 to 6 times and the end of the string indicated by $. meaning this is the part checking for the top level domain.
your regex would accept: blah#google.com aswell as blah#google.com.com
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed last year.
Improve this question
I wanted to find a regex pattern that checks if there's one letter in between the space.
For example: John A Doe
I want to capture only John Doe (without A) but there's 50/50 chance that the data will not contain middle initials.
I made this pattern ([A-z]* [A-z] [A-z]*|[A-z]* [A-z]*) but it captures also the middle one.
I'm sorry for the vague title cause I'm really confused rn.
Edit: I forgot about the [A-z] captures upto 122 in ascii table. I replaced it with \w as well.
Perhaps code like:
var m = Regex.Match(input, #"(?<f>[a-z]+)( [a-z])? (?<l>[a-z]+)", RegexOptions.IgnoreCase);
Console.WriteLine(m.Groups["f"].Value);
Console.WriteLine(m.Groups["l"].Value);
You'll need to capture the first and last names separately and stick them back together later
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
What would be a regex for matching "[d-n]", where n is any number?
i.e.
Test_4_[d-123] - returns ideally only 123
or, if I can return [d-123] I could make some string formatting.
(?<=\[d-)\d+(?=\]) will return 123 from Test_4_[d-123].
You give very little information as to how you want stuff matched.
A very simple solution could be:
\[d-(\d+)\]
From left to right:
\[ will match the literal character [
d- will match just d-
(\d+) will match any digit one or more times. It is in parenthesis, which makes it a capture/match group. This should mean that if you are using a regex tool/library, you should be able to retrieve the "first match group" and you should retrieve just 123.
\] will match the literal character ]
I can only suggest using a website like https://regex101.com/ which can help in creating regular expressions.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Basically I want to check if a String is a Sentence ("Hello, I am Me!") or Symbol Spam ("HH,,,{''{"), without using the number of symbols as a factor as much as possible. Right now it just detects based on a counter of symbols, but when someone says something with lots of punctuation, they get kicked.
Help?
If the number of symbols in the text is not sufficient, and you don't want to use something too fancy (or bought) could I suggest implementing one or more of these further steps (of increasing difficulty):
Make a count of all A-Za-z and space characters in the string and make a ratio of this to the count of symbols - so if they write a sentence then !!!!!!!!!!!!! at the end it still doesn't snag as the ratio is high enough.
If this still isn't discerning enough, add a further check if you pass item 1...
Count numbers of consecutive A-Za-z characters in the string - work out the average length of these 'words' - if the average is too short then it is probably spam.
These can be done in RegEx reasonably easily - If you want more sophistication then you have to use something written by someone else that has much more developed statistical methods (or start reading lexographical university papers that are beyond me!)
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
In my "ServiceEditModel" class i have a property Url with typeof Uri. For validation I search a Regex that check if the Url, which is filled up on my "Edit" page, is valid.
The Regex should check
Is there a http:// or https://
That the body only contains alphabetic characters and numbers
And the ending is like for example .com, .net, .ch
It should be possible, that there is another parameter behind the ending like for example https://stackoverflow.com/questions
My Code where the Regex comes in look like this:
[Required(ErrorMessageResourceType = typeof(Resources.ApplicationTemplate), ErrorMessageResourceName = "UrlRequired")]
[RegularExpression("REGEX COMES HERE", ErrorMessageResourceType = typeof(Resources.ApplicationTemplate), ErrorMessageResourceName = "InvalidUrl")]
public Uri Url { get; set; }
I already looked for Regex but can't find the right one because this is actually my first experiance with Regex.
Thanks for Help!
EDIT
I updated my regex so that it also allows url's with a "-" character such as http://www.comsoft-direct.ch/
Updated regex: ^(http|https):\/\/([\w\d + (\-)+?]+\.)+[\w]+(\/.*)?$
This Regex should check simple scenarios according to your constraints. You can easily play with it and improve it (which I strongly recommend, firstly because it's very simple at this state and secondly because you are a Regex beginner :)).
^(http|https):\/\/[\w\d]+\.[\w]+(\/[\w\d]+)$
Check it on Regex 101
Basic explanation:
(http|https):\/\/
Should start with http or https, followed by ://
[\w\d]+
Followed by N letters and/or digits
\.[\w]+
Followed by a dot and a set of letters. e.g.: .com, .net and such (note that you must change to \.[\d\w]+ to allow digits also)
(\/[\w\d]+)
Followed, optionally, by a / and a set of letters and/or digits (e.g.: /questions)
NOTE: If you want a full-generic url validator, you must then google for that.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I need one white space only in my regular expressen.
How should I create the code that validate if one white space is availabe in the string value?
I'm not sure exactly what you mean but I'm guessing you want to check for exactly one whitespace, but any number of non-whitespace characters:
#"^\S*\s\S*$"
Example code:
Regex regex = new Regex(#"^\S*\s\S*$");
Console.WriteLine(regex.IsMatch("Hello, world!"));
Console.WriteLine(regex.IsMatch("This contains three spaces."));
Console.WriteLine(regex.IsMatch("Two\nlines."));
Output:
True
False
True
Other variations
To check if the string contains exactly one whitespace only (no other characters):
#"^\s$"
To check if the string contains at least one whitespace:
#"\s"