|
My friend is making windows form and he needs to vlidate a username which will be a minimum of 4 characters and maximum of 15 characters long. It will also allow hyphens and underscores as well as dots in the middle, but not at the start and neither at the end of the username. There may be no more than one hyphen, one underscore and one dot in a row
Examples of disallowed usernames:.
-aquib
_aquib
.aquibxyz
aquib.
aquibxyz--qureshi
aquib__xyzqureshi
aquibqureshi-
aquib_qureshi-qureshi
aquib..qureshi
1236584 // not allow only numbers in username
aquib_ // means no symbols will be there at end
The username should not be only digits it should be either a mix of digits and alphabetical characters or it should be only alphabetic.
I hope this will be understood
I have got this regex:
^([a-zA-Z0-9](?(?!__|--)[a-zA-Z0-9_\-]){0,4}[a-zA-Z0-9])$
which is not usefull enough
|
|
|
|
|
|
yup. Thankx but can you simply provide me with such a regex which will take only numbers and alphabets only. (no special chrecters and dot and hifens and underscore all symbols are not allowed)
Minimum 3 and maximum 15 characters length must be and
and cannot take only numbers it should consist of only alphabets and numbers or only alphabets
( a username should not b only numbers it should be alphanumeric)
and i have downloaded expresso its too difficult to understand. I am a beginer. Of c#
sorry i must have frustated many coders
: (
|
|
|
|
|
If you take a look at The 30 Minute Regex Tutorial[^] here on CodeProject, you can work it out fairly easily to be something like:
\b\w{3,15}\b
You may need to modify the above for your specific requirements but it should get you started.
One of these days I'm going to think of a really clever signature.
|
|
|
|
|
OK, I've now worked out how to do it almost perfectly. For explanation purposes I've split the expression into three lines, but if you will use it it will have to written in one line like below:
(?!^[a-zA-Z0-9\.\-_]{1,2}$)(?!^[a-zA-Z0-9\.\-_]{16,}$)(?!^[0-9]{3,15}$)^(?:[a-zA-Z0-9](?:\.|_|-)?)+[a-zA-Z0-9]$
Explanation:
There is a grouping operator for regular expressions that is called zero-width negative lookahead (?! < subexpression >). What does that mean? Let's dissect this monster description:
- Zero-width lookahead
- This essentially means that this matching expression will not consume any characters. So when this expression has matched any following non zero.width expression will start of at the same place in the string as the zero-width regular expression did.
- Negative
- This signifies that all following expressions can only match, if this expression did not produce any matches.
(?!^[a-zA-Z0-9\.\-_]{1,2}$) // This expression will assert that a username with characters a-z A-Z 0-9 . _ and - are not less than three characters long
(?!^[a-zA-Z0-9\.\-_]{16,}$) // This expression will assert that a username with characters a-z A-Z 0-9 . _ and - are no more than 15 characters long
(?!^[0-9]{3,15}$) // This expression will assert that a username will not be made up of only digits [0-9]
^(?:[a-zA-Z0-9](?:\.|_|-)?)+[a-zA-Z0-9]$
Given the three assertions at the beginning of the regular expression we can now be sure that there are at least 3 characters and no more than 15 and that a username of only digits is also disallowed. The ^ and $ in the assertions are important here.
Now lets have a look at the final part which is also broken into several lines to better annotate it:
^
(?:[a-zA-Z0-9]
(?:\.|_|-)?
)+
[a-zA-Z0-9]
$
I do hope you could follow my explanations and found this solution helpful. The only drop of bitternes to me is that we now have an additiona constraint:
While it is true that the each of the characters '.', '-' and '_' may appear no more than once in row (valid constraint from OP), but now none of the charactes in the class [\.\-_] can appear one after another (new constraint introduced through solution).
Regards,
— Manfred
"I had the right to remain silent, but I didn't have the ability!"
Ron White, Comedian
|
|
|
|
|
it does take
shariq_k.-kha, how to avoid that. I mean allowing only one dot dash or underscore in a row?
|
|
|
|
|
You must be doing something wrong. It works exactly like you want it.
"I had the right to remain silent, but I didn't have the ability!"
Ron White, Comedian
|
|
|
|
|
but bro i told about not more that one symbols. means not more that one hifen or not more than one underscore or not more than dot. and not the combination should not be more than once means
shariq_khan.k
do you have checked or misunderstood?
|
|
|
|
|
Above you said: "I mean allowing only one dot dash or underscore in a row?".
The meaning of "in a row" is that the characters consecutively follow one another with out other characters in between.
If you meant that there should be at most one "-" and at most one "." and at most one "_", you should have said so.
I don't think this is possible to achieve using regular expressions. You'd have to iterate over the characters and increment a counter for each of the three characters "-", "." and "_". As soon a one of the counters is greater than one the check already failed and you don't need to use the regulare expression.
Regards,
Manfred
"I had the right to remain silent, but I didn't have the ability!"
Ron White, Comedian
|
|
|
|
|
ohkay.bro thanks.
and how can i contact you here on code project?
is there any message system here?
|
|
|
|
|
You can leave me a message on my messaged board on my profile page.
Cheers!
"I had the right to remain silent, but I didn't have the ability!"
Ron White, Comedian
|
|
|
|
|
Same negative trick works, doesn't it?
(?!\.[^.]*\.)(?!\-[^-]*\-)(?!\_[^_]*\_)
Or (?![.-_][^.-_]*[.-_]) if he wants at most one of any of the three.
Cheers,
Peter
Software rusts. Simon Stephenson, ca 1994. So does this signature. me, 2012
|
|
|
|
|
Just a quick thought. Why not handle repeated . - _ with negatives? Something like (?!\.\.)(?!\-\-)(?!\_\_) or (?!\.\.|\-\-|\_\_)
I've had this idea kicking around the back of my heads since another member posted somewhere that you can't do [a similar thing] with regex.
Otherwise an excellent answer and +5
Cheers,
Peter
Software rusts. Simon Stephenson, ca 1994. So does this signature. me, 2012
|
|
|
|
|
I'm trying to write a scanner using Flex.
The problem is that one of my regex's works in the Visual Studio Find dialog, but not in Flex when tested on the same file!
By "not working" I mean that it doesn't find any matches.
\'((.)|(\\['"?\\abfnrtv])|(\\[0-7]{1,3})|(\\[Xx][0-9a-fA-F]+))\'
This regex is supposed to recognize any character literals, like 'a' , '\t' , '\012' , or '\x01AF' .
Can anyone suggest why this regex is not finding any matches in Flex?
SOLVED: It turns out that I had other rules that were consuming the character literals earlier in the process.
The difficult we do right away...
...the impossible takes slightly longer.
modified 6-Sep-12 16:11pm.
|
|
|
|
|
Taking a quick look at the (not terribly impressive) Flex docco, I'd suggest you try the Posix-compliance switch to Flex to see if that helps. Either way, you'll have eliminated some variables.
fwiw, Expresso thinks you regex is OK.
Cheers,
Peter
Software rusts. Simon Stephenson, ca 1994. So does this signature. me, 2012
|
|
|
|
|
Peter_in_2780 wrote: Expresso thinks you regex is OK.
Thanks for your help.
I solved it.
It turns out to be a case of the computer doing what I told it to do instead of what I wanted it to do.
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
Hi,
I have file lectures and I would like to change the nameing convention.
Currently, it has the format of {detail -} subject - author.wmv
I would like to rename with the nameing convention of author - subject { - detail}.wmv
Some observation, initialy, the author is at the end of the file name preceded by a seperator of minus sign.
{detail -} is optional and thus some file name utilize. Thus, we can have more than one minus sign as seperator but author name is always at end of file name preceded by the minus sign seperator.
Example file names;
e4 e5 - The Four Kn1ghts for Black - Vig0rito.wmv
e4 e5 The Sc0tch for Black - Part 1 - Vig0rito.wmv
2+1=2; A Practical Endgam3 Example from L1nares - P@schall.wmv
Result ( I would like. That is, lecturer seperator topic )
Vig0rito - e4 e5 - The Four Kn1ghts for Black.wmv
Vig0rito - e4 e5 The Sc0tch for Black - Part 1.wmv
P@schall - 2+1=2; A Practical Endgam3 Example from L1nares.wmv
Purpose, I would like to access material by author.
Thank in advance for your help.
|
|
|
|
|
You can use System.IO.Path.GetFileNameWithoutExtension() . Then find the position of the last "-" with the LastIndexOf('-') function of the string class. Build up your new string from the respective Substring() s.
|
|
|
|
|
Thank you for your reply Bernhard and introduction to system io.
Currently, I am using a free file renaming tool that utilize regexp. After looking at the coding and re-familiarizing myself with regexp and having no luck, I made a post. The next morning, I came with a quasi-solution. I do not want to spam the tool but it is the most complex rfr*k according to review.
What unique about this file title renaming is the number of seperators can vary. Thus, mp3 titles with challenging non-fix number of seperators would find this thread informative. I am stump trying to access the last seperator with $ anchor to create an expression and thus sent this sos.
Here is my quasi-solution and hope it helps some with similar problem.
(?x)(.*?)[ ]*?[-]+[ ]*?(.*) [ ]*?[-]+[ ]*?(.*) # start of swap 2 fields \3 zzzz \2 zzzz \1
(?x)[ ]*[-][ ] # Handle Linaries example _
(?x)(.*?)[ ]*?[_]+[ ]*?(.*)$ # Linaries example \2 zzzz \1
\bzzzz\b # Convert back to minus sign -
(.?)\bwmv\b # remove the .wmv after the author name to null
I use zzzz and _ as temporary strings with the understanding that they are unique in my files. Adapt accordingly. I adapted from a swap 2 fields expression. The five lines and replace work for my demo example. Thanks.
Edit: Some minor quirks but liveable. tx
modified 6-Aug-12 23:05pm.
|
|
|
|
|
OK, that's different background.
In that case, I'd rely on the "greedy" character of wild cards.
(?<part1>.*)\s-\s(?<part2>.*)(?<ext>\.wmv) will catch the parts and the extension,
and the re-order it to become part2 - part1.ext
|
|
|
|
|
To cut down the detail, here is my input:
[char](15)
[varchar](23)
So the first field is a square bracket '[', a string and a close bracket ']'. Next is a regular bracket '(', a number, and a close bracket ')'.
I want to use Emacs to replace so here is the result:
15 char
23 varchar
So take out all those bracket or square bracket. Number goes first, a space and the string.
How to do it? I know how to swap values, but adding the pattern of string, I don't know.
|
|
|
|
|
Use (automatically) numbered capture groups:
Matching regex
\[(\S*)\]\((\d*)\)
will capture a string of non-whitepace between [ and ] into $1 and the following number in ( ) into $2.
Your replacement string is then
$2 $1
Peter
Software rusts. Simon Stephenson, ca 1994. So does this signature. me, 2012
|
|
|
|
|
Sorry Peter, does not work in Emacs 23.3.1 Windows version.
When typing in the search string, Emacs does not complain, so it seems like working.
But it just does not do the replace.
|
|
|
|
|
I'm not familiar with Emacs (in the last 25 years, anyway). It seems to use a very different regex engine from the ones we meet here (in .NET languages, PHP, Java and so on). You might do better to ask in an Emacs forum.
Sorry I can't help any more.
Peter
Software rusts. Simon Stephenson, ca 1994. So does this signature. me, 2012
|
|
|
|
|