정규 표현 식 문자열 을 자 르 는 방법
[숫자]문자열
결실
취하 다 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;
}
}
이상 은 본문 에서 공유 한 정규 표현 식 에서 문자열 을 자 르 는 방법 입 니 다.여러분 이 좋아 하 시 기 를 바 랍 니 다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
awk 상용 명령awk 는 모든 입력 줄 을 하나의 기록 으로 인식 하고 그 줄 의 모든 단어 도 메 인 을 하나의 필드 로 인식 합 니 다. ARGC 명령 줄 에 awk 스 크 립 트 가 들 어 오 는 매개...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.