Click here to Skip to main content
15,566,623 members
Home / Discussions / Regular Expressions
   

Regular Expressions

 
GeneralRe: Parsing ASCII-Only string Pin
Marco Bertschi7-Feb-14 2:50
professionalMarco Bertschi7-Feb-14 2:50 
Richard,

thank you very much for your answer. I have developed the Regex a bit further, so it parses only matching strings, causing an InvalidOperationException in case the string contains any invalid characters. This behavior makes sense, since a hostname with removed characters may cause confusion.
I have also added a length maximum of 255 characters, and the string must have at least a single character.

C#
Regex regex = new Regex(@"(^[\40-\176]{1,254}$)");//Printable ASCII characters only
Match match = regex.Match(value);
if (match.Groups.Count == 2)
{
    host = match.Groups[1].Value;
}
else
{
    throw new InvalidOperationException(string.Format("The string {0} for host is misformatted. It may only contain printable US-ASCII characters.", value));
}


I am not fully convinced by your idea of iterating through the string - The RegEx makes the code easier to read, and easier to change if someone suddenly requires to allow special characters. In Addition to that, I fear a performance loss when iterating through the string.
Clean-up crew needed, grammar spill... - Nagy Vilmos

GeneralRe: Parsing ASCII-Only string Pin
Richard Deeming7-Feb-14 3:04
mveRichard Deeming7-Feb-14 3:04 
QuestionRe: Parsing ASCII-Only string Pin
Marco Bertschi7-Feb-14 3:08
professionalMarco Bertschi7-Feb-14 3:08 
AnswerRe: Parsing ASCII-Only string Pin
Richard Deeming7-Feb-14 3:14
mveRichard Deeming7-Feb-14 3:14 
GeneralRe: Parsing ASCII-Only string Pin
Marco Bertschi7-Feb-14 3:33
professionalMarco Bertschi7-Feb-14 3:33 
QuestionParsing a string with match groups Pin
Dominick Marciano23-Aug-13 10:47
professionalDominick Marciano23-Aug-13 10:47 
AnswerRe: Parsing a string with match groups Pin
Matt T Heffron28-Oct-13 12:33
professionalMatt T Heffron28-Oct-13 12:33 
AnswerRe: Parsing a string with match groups Pin
Gabriel Szabo7-Nov-13 22:27
Gabriel Szabo7-Nov-13 22:27 
QuestionMatching a string. Pin
Septimus Hedgehog19-Aug-13 5:48
Septimus Hedgehog19-Aug-13 5:48 
AnswerRe: Matching a string. Pin
PIEBALDconsult19-Aug-13 6:03
professionalPIEBALDconsult19-Aug-13 6:03 
GeneralRe: Matching a string. Pin
Septimus Hedgehog19-Aug-13 6:27
Septimus Hedgehog19-Aug-13 6:27 
GeneralRe: Matching a string. Pin
PIEBALDconsult19-Aug-13 6:31
professionalPIEBALDconsult19-Aug-13 6:31 
GeneralRe: Matching a string. Pin
Septimus Hedgehog19-Aug-13 6:44
Septimus Hedgehog19-Aug-13 6:44 
GeneralRe: Matching a string. Pin
PIEBALDconsult19-Aug-13 10:01
professionalPIEBALDconsult19-Aug-13 10:01 
GeneralRe: Matching a string. Pin
Septimus Hedgehog20-Aug-13 0:04
Septimus Hedgehog20-Aug-13 0:04 
QuestionHelp wanted with regex format. Pin
Septimus Hedgehog31-Jul-13 4:17
Septimus Hedgehog31-Jul-13 4:17 
AnswerRe: Help wanted with regex format. Pin
Richard Deeming31-Jul-13 4:47
mveRichard Deeming31-Jul-13 4:47 
GeneralRe: Help wanted with regex format. Pin
Septimus Hedgehog31-Jul-13 4:53
Septimus Hedgehog31-Jul-13 4:53 
AnswerRe: Help wanted with regex format. Pin
OriginalGriff31-Jul-13 5:10
mvaOriginalGriff31-Jul-13 5:10 
GeneralRe: Help wanted with regex format. Pin
Septimus Hedgehog31-Jul-13 5:34
Septimus Hedgehog31-Jul-13 5:34 
GeneralRe: Help wanted with regex format. Pin
OriginalGriff31-Jul-13 5:40
mvaOriginalGriff31-Jul-13 5:40 
GeneralRe: Help wanted with regex format. Pin
Septimus Hedgehog31-Jul-13 6:15
Septimus Hedgehog31-Jul-13 6:15 
GeneralRe: Help wanted with regex format. Pin
Bernhard Hiller31-Jul-13 23:26
Bernhard Hiller31-Jul-13 23:26 
GeneralRe: Help wanted with regex format. Pin
Septimus Hedgehog1-Aug-13 0:58
Septimus Hedgehog1-Aug-13 0:58 
GeneralRe: Help wanted with regex format. Pin
Bernhard Hiller1-Aug-13 21:39
Bernhard Hiller1-Aug-13 21:39 

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.