정규 표현 식 문자열 을 자 르 는 방법

다음 문자열 이 있 습 니 다:
[숫자]문자열
결실
취하 다  a=숫자
     b=문자열
캡 처 방법 1:

int a = Convert.ToInt32(txt1.Text.Trim().Replace('[', ']').Split(']')[1]);
   string b = txt1.Text.Trim().Replace('[', ']').Split(']')[2]; 
캡 처 방법 2:

string str = "[  ]   ";
Regex reg = new Regex(@"
([^]+)\](.*)");
string a= Convert.ToInt32( reg.Match(str).Groups[1].Value);
string b= Convert.ToInt32( reg.Match(str).Groups[2].Value);
절취 방법

string tempStr = "[  ]   "; 
string pattern = @"
([\s§]∗)
([\s\S]*)";
Regex re = new Regex(pattern); 
string str1 = Regex.Replace(tempStr,pattern,"$1"); 
string str2 = Regex.Replace(tempStr, pattern, "$2");
  배열 이 되면 어떻게 써 요?

  /// <summary>
  ///          
  /// </summary>
  /// <param name="str"></param>
  /// <returns></returns>
  public string[] ReturnIDAndName(string str)
  {    
    string[] stringArray = new string[2];    
    Regex reg = new Regex(@"
([^]+)\](.*)");
    stringArray[0]= reg.Match(str).Groups[1].Value;
    stringArray[1] = reg.Match(str).Groups[2].Value;    
    return stringArray;
  } 
 
  /// <summary>
  ///        
  /// </summary>
  public int ReturnId(string str)
  {
    try
    {
      if (string.IsNullOrEmpty(str))
      {
        return 0;
      }
      Regex regex = new Regex("(?<=\\[)\\d+(?=\\])");
      Match m = regex.Match(str);
      int pid;
      if (!m.Success)
      {
        pid = int.Parse("[" + regex.Match(str).Value + "]");
      }
      return int.Parse(regex.Match(str).Value);
    }
    catch
    {
      return 0;
    }
  }
이상 은 본문 에서 공유 한 정규 표현 식 에서 문자열 을 자 르 는 방법 입 니 다.여러분 이 좋아 하 시 기 를 바 랍 니 다.

좋은 웹페이지 즐겨찾기