2017-08-25 100 views
0

我看到一個類似的主題(this),但無法達到理想的解決方案。正則表達式替換數字和只有一個連字符C#

我需要什麼:

,關於keypress事件TextBox""更換非數字和過度連字符的工作的面具。

允許:

enter image description here

什麼是我的難處?

檢查相同表達式中只有一個連字符的輸入。

我使用子串進入解決方案,它只在KeyUP工作,但我想通過使用表達式和keypress事件。

我已經嘗試過:

using System.Text.RegularExpressions; 

    //trying to denie non-digit and hyphen. 
    //In conjunction with replace I remove everything that is not hyphen and digit 
    private static Regex MyMask = new Regex(@"[^\d-]"); 


    private void inputSequential_KeyUp (object sender, KeyEventArgs e) 
    { 
     if (! String.IsNullOrEmpty (inputSequential.Text) 
     { 
      inputSequential.Text = MyMask.Replace (inputSequential.Text, ""); 

      // MatchCollection matches = Regex.Matches (inputSequential.Text, "[\\ -]"); 
      // 
      // if (matches.Count> 1) 
      // { 
      // for (int i = 1; i <= matches.Count - 1; i ++) 
      // { 
      // inputSequential.Text = inputSequential.Text.Substring (0, matches [i] .Index-1) + inputSequential.Text.Substring (matches [i] .Index, inputSequential.Text.Length); 
      // inputSequential.Text = inputSequential.Text.Replace (inputSequential.Text [matches [i] .Index] .ToString(), ""); 
      //} 
      //} 
     } 
    } 

預計:

enter image description here

如果你知道更好的方法來做到這一點,請讓我知道。 謝謝傾聽。

+0

你這裏正則表達式是搞砸了。這是一個複製/粘貼錯誤? –

+0

所以,你試圖捕獲一個序列:一個可選的':'和一個數字和'-'以外的字符。這是錯誤的。 –

+0

在我的理解中,這個表達式拒絕非數字和連字符。結合替換我刪除不是連字符和數字的一切。 –

回答

0

我搜索這裏一些替代品,用正則表達式或MaskedTextBox中(這並沒有幫助我很多,因爲默認情況下它不會在工具條支持地方我的textBox是)。

在我找到了最好的解決方案正在處理值的每一個字符輸入的一天結束:

private void inputSequencial_KeyPress(object sender, KeyPressEventArgs e) 
{ 
    //Allow only digits(char 48 à 57), hyphen(char 45), backspace(char 8) and delete(char 127) 
    if ((e.KeyChar >= 48 && e.KeyChar <= 57) || e.KeyChar == 45 || e.KeyChar == 8 || e.KeyChar == 127) 
    {  
     switch (e.KeyChar) 
     { 
     case (char)45: 
     int count = inputSequencial.Text.Split('-').Length - 1; 
     //If the first char is a hyphen or 
     //a hyphen already exists I reject the entry 
     if (inputSequencial.Text.Length == 0 || count > 0) 
     { 
      e.Handled = true; 
     } 
     break; 
     } 
    } 
    else 
    { 
     e.Handled = true; //Reject other entries 
    } 
} 

private void inputSequencial_KeyUp(object sender, KeyEventArgs e) 
{ 
    //if last char is a hyphen i replace it. 
    if (inputSequencial.Text.Length > 1) 
    { 
     string lastChar = inputSequencial.Text.Substring(inputSequencial.Text.Length - 1, 1); 
     if (lastChar == "-") 
     { 
     inputSequencial.Text.Replace("-", ""); 
     } 
    } 
} 
0

你似乎在失去:

那表情:(:?) - 不是nonmatched組。正確的變體是:(?:)

digitsOnly - 這將是\d?

你不應該逃避-

如果你正在尋找一個-,只是把它寫下來。

對於正則表達式 - 更好地寫下來的文字,你在找什麼。對於排除或接受,並不重要,但是用英語說,你需要什麼。

請寫下應該被接受的例子和這些不應該被接受的例子。

爲獲得唯一號碼,可能與 - 之前,使用:

-?\d+ 

tests

+0

With @「 [^ \ d-]「我試圖拒絕非數字和連字符。 結合替換我刪除不是連字符和數字的一切 –

1

可以使用LINQ表達式來得到的只有數字和一個連字符:

string input = "12-3-47--Unwanted Text"; 
int hyphenCount = 0; 
string output = new string(input.Where(ch => Char.IsNumber(ch) || (ch == '-' && hyphenCount++ < 1)).ToArray()); 
0

您可以將其用於非數字[^ \ d]:

var st = "1kljkj--2323'sdfkjasdf2"; 
var result = Regex.Replace(st, @"^(\d).*?(-)[^\d]*(\d+)[^\d]*", @"$1$2$3"); 

1-23232