Click here to Skip to main content
14,930,079 members
Home / Discussions / Regular Expressions
   

Regular Expressions

 
GeneralRe: A little help needed... Pin
Marco Bertschi20-Mar-14 7:02
professionalMarco Bertschi20-Mar-14 7:02 
AnswerRe: [Solved] Pin
Kenneth Haugland20-Mar-14 20:55
professionalKenneth Haugland20-Mar-14 20:55 
GeneralRe: [Solved] Pin
PIEBALDconsult25-Mar-14 12:30
professionalPIEBALDconsult25-Mar-14 12:30 
QuestionMATLAB Code Pin
Member 1068110218-Mar-14 19:02
MemberMember 1068110218-Mar-14 19:02 
QuestionRe: MATLAB Code Pin
Richard Deeming19-Mar-14 2:04
mveRichard Deeming19-Mar-14 2:04 
AnswerRe: MATLAB Code Pin
Kornfeld Eliyahu Peter19-Mar-14 5:00
professionalKornfeld Eliyahu Peter19-Mar-14 5:00 
QuestionParsing ASCII-Only string Pin
Marco Bertschi6-Feb-14 22:54
professionalMarco Bertschi6-Feb-14 22:54 
AnswerRe: Parsing ASCII-Only string Pin
Richard Deeming7-Feb-14 1:36
mveRichard Deeming7-Feb-14 1:36 
I'm not convinced by your Regex - you're matching the literal string " -~" and capturing it to a group. To indicate a range of characters, you need some square brackets:
([\x20-\x7e])

(I prefer to use hex values, since it's more obvious that they're not decimal values; you don't have to remember that the default is octal.)

Since you just want to remove any characters that aren't in the specified range, the simplest approach is to negate the range and use the Replace method:
set
{
    host = Regex.Replace(value, @"[^\x20-\x7e]", string.Empty);
}


However, for a simple case like this, Regex is probably overkill; you could simply loop through the characters, discarding any that you don't want:
set
{
    if (!string.IsNullOrEmpty(value))
    {
        char[] validChars = new char[value.Length];
        int validCharIndex = 0;
        
        foreach (char c in value)
        {
            if ('\x20' <= c && c <= '\x7e')
            {
                validChars[validCharIndex] = c;
                validCharIndex++;
            }
        }
        
        if (validCharIndex == 0)
        {
            value = string.Empty;
        }
        else if (validCharIndex != value.Length)
        {
            value = new string(validChars, 0, validCharIndex);
        }
    }
    
    host = value;
}




"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer


GeneralRe: Parsing ASCII-Only string Pin
Marco Bertschi7-Feb-14 1:50
professionalMarco Bertschi7-Feb-14 1:50 
GeneralRe: Parsing ASCII-Only string Pin
Richard Deeming7-Feb-14 2:04
mveRichard Deeming7-Feb-14 2:04 
QuestionRe: Parsing ASCII-Only string Pin
Marco Bertschi7-Feb-14 2:08
professionalMarco Bertschi7-Feb-14 2:08 
AnswerRe: Parsing ASCII-Only string Pin
Richard Deeming7-Feb-14 2:14
mveRichard Deeming7-Feb-14 2:14 
GeneralRe: Parsing ASCII-Only string Pin
Marco Bertschi7-Feb-14 2:33
professionalMarco Bertschi7-Feb-14 2:33 
QuestionParsing a string with match groups Pin
Dominick Marciano23-Aug-13 9:47
professionalDominick Marciano23-Aug-13 9:47 
AnswerRe: Parsing a string with match groups Pin
Matt T Heffron28-Oct-13 11:33
professionalMatt T Heffron28-Oct-13 11:33 
AnswerRe: Parsing a string with match groups Pin
Gabriel Szabo7-Nov-13 21:27
MemberGabriel Szabo7-Nov-13 21:27 
QuestionMatching a string. Pin
Septimus Hedgehog19-Aug-13 4:48
MemberSeptimus Hedgehog19-Aug-13 4:48 
AnswerRe: Matching a string. Pin
PIEBALDconsult19-Aug-13 5:03
professionalPIEBALDconsult19-Aug-13 5:03 
GeneralRe: Matching a string. Pin
Septimus Hedgehog19-Aug-13 5:27
MemberSeptimus Hedgehog19-Aug-13 5:27 
GeneralRe: Matching a string. Pin
PIEBALDconsult19-Aug-13 5:31
professionalPIEBALDconsult19-Aug-13 5:31 
GeneralRe: Matching a string. Pin
Septimus Hedgehog19-Aug-13 5:44
MemberSeptimus Hedgehog19-Aug-13 5:44 
GeneralRe: Matching a string. Pin
PIEBALDconsult19-Aug-13 9:01
professionalPIEBALDconsult19-Aug-13 9:01 
GeneralRe: Matching a string. Pin
Septimus Hedgehog19-Aug-13 23:04
MemberSeptimus Hedgehog19-Aug-13 23:04 
QuestionHelp wanted with regex format. Pin
Septimus Hedgehog31-Jul-13 3:17
MemberSeptimus Hedgehog31-Jul-13 3:17 
AnswerRe: Help wanted with regex format. Pin
Richard Deeming31-Jul-13 3:47
mveRichard Deeming31-Jul-13 3:47 

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.