Click here to Skip to main content
15,881,882 members
Home / Discussions / Regular Expressions
   

Regular Expressions

 
AnswerRe: Extract tag within pre tag Pin
BobJanova2-Oct-12 1:19
BobJanova2-Oct-12 1:19 
GeneralRe: Extract tag within pre tag Pin
DATHeike2-Oct-12 1:23
DATHeike2-Oct-12 1:23 
GeneralRe: Extract tag within pre tag Pin
Pete O'Hanlon2-Oct-12 1:32
mvePete O'Hanlon2-Oct-12 1:32 
Questionhow to make username regex for windows forms Pin
sariqkhan30-Sep-12 21:20
sariqkhan30-Sep-12 21:20 
AnswerRe: how to make username regex for windows forms Pin
Kenneth Haugland30-Sep-12 22:32
mvaKenneth Haugland30-Sep-12 22:32 
GeneralRe: how to make username regex for windows forms Pin
sariqkhan30-Sep-12 23:11
sariqkhan30-Sep-12 23:11 
GeneralRe: how to make username regex for windows forms Pin
Richard MacCutchan30-Sep-12 23:44
mveRichard MacCutchan30-Sep-12 23:44 
AnswerRe: how to make username regex for windows forms Pin
Manfred Rudolf Bihy2-Oct-12 2:11
professionalManfred Rudolf Bihy2-Oct-12 2:11 
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:

  1. 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.
  2. 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:

^                // Start match at the beginning to the string
(?:[a-zA-Z0-9]   // Make sure that a username will not start with a '.' a '-' or a '_', but only alphanumeric characters
    (?:\.|_|-)?  // After an initial alpha numeric a '.' a '-' or a '_' may follow, but not two in a row ...
)+               // ... so we repeat this pattern once or many (+). Drawback: ._ .- _- etc. are also not allowed, which I think we can live with here.
[a-zA-Z0-9]      // Finally we make sure that there is an alphanumeric character in the last position ...
$                // ... exactly at the end of the string.


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


GeneralRe: how to make username regex for windows forms Pin
sariqkhan2-Oct-12 7:22
sariqkhan2-Oct-12 7:22 
GeneralRe: how to make username regex for windows forms Pin
Manfred Rudolf Bihy3-Oct-12 23:10
professionalManfred Rudolf Bihy3-Oct-12 23:10 
GeneralRe: how to make username regex for windows forms Pin
sariqkhan3-Oct-12 23:27
sariqkhan3-Oct-12 23:27 
GeneralRe: how to make username regex for windows forms Pin
Manfred Rudolf Bihy4-Oct-12 0:44
professionalManfred Rudolf Bihy4-Oct-12 0:44 
GeneralRe: how to make username regex for windows forms Pin
sariqkhan4-Oct-12 0:55
sariqkhan4-Oct-12 0:55 
GeneralRe: how to make username regex for windows forms Pin
Manfred Rudolf Bihy4-Oct-12 1:01
professionalManfred Rudolf Bihy4-Oct-12 1:01 
GeneralRe: how to make username regex for windows forms Pin
Peter_in_27804-Oct-12 2:51
professionalPeter_in_27804-Oct-12 2:51 
GeneralRe: how to make username regex for windows forms Pin
Peter_in_27802-Oct-12 12:36
professionalPeter_in_27802-Oct-12 12:36 
Question[SOLVED] C Code Parsing Pin
Richard Andrew x645-Sep-12 16:03
professionalRichard Andrew x645-Sep-12 16:03 
AnswerRe: C Code Parsing Pin
Peter_in_27805-Sep-12 16:45
professionalPeter_in_27805-Sep-12 16:45 
GeneralRe: C Code Parsing Pin
Richard Andrew x646-Sep-12 10:09
professionalRichard Andrew x646-Sep-12 10:09 
QuestionRename File Name Help Pin
toikken5-Aug-12 18:56
toikken5-Aug-12 18:56 
AnswerRe: Rename File Name Help Pin
Bernhard Hiller5-Aug-12 23:11
Bernhard Hiller5-Aug-12 23:11 
GeneralRe: Rename File Name Help Pin
toikken6-Aug-12 15:05
toikken6-Aug-12 15:05 
GeneralRe: Rename File Name Help Pin
Bernhard Hiller6-Aug-12 20:24
Bernhard Hiller6-Aug-12 20:24 
QuestionSwap two values based on pattern Pin
biop.codeproject2-Aug-12 17:19
biop.codeproject2-Aug-12 17:19 
AnswerRe: Swap two values based on pattern Pin
Peter_in_27802-Aug-12 19:02
professionalPeter_in_27802-Aug-12 19:02 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.