다음을 감지하는 정규식을 알고 싶습니다 (특히 C #의 경우).
문자열이 간단한 대괄호 안의 텍스트로 끝나는 지 여부. 예를 들면 :
일부 일치 :
this is a string (dsdfgfg)
this is a (string (123456)
다음은 문자열이 간단한 대괄호로 끝나는 지 여부를 감지하는 샘플 코드입니다.
static void Main(string[] args)
{
const string s = "this is a sentence (367662288)";
var result = Regex.IsMatch(s, @"\)$");
Console.WriteLine(result); // true
}
일치하지 않는 일부 :
this is a string (fdf
this is a string (dsdfgfg) temp
this is a string (dsdfgfg))
그건 그렇고 닫는 간단한 대괄호 뒤에 공백이 허용되지만 다른 문자는 허용되지 않습니다.
감사
로 @"\)$"
끝나는 문자열 만 일치 )
합니다. 당신은 그것을 쓰는 것이 s.EndsWith(")")
좋을 것이고 당신은 같은 결과를 얻을 것입니다.
당신이 사용할 수있는
@"\([^()]*\)$"
정규식 데모를 참조하십시오 (는 무시하고 \r?
데모 전용입니다).
정규식 일치
\(
-개방형 원형 브래킷[^()]*
-a (
및 이외의 0 개 이상의 문자)
\)
-닫는 둥근 괄호$
-문자열 끝.C # 데모 :
using System;
using System.Text.RegularExpressions;
using System.IO;
public class Test
{
private static readonly Regex rx = new Regex(@"\([^()]*\)$", RegexOptions.Compiled);
public static void Main()
{
var strs = new string[] {"this is a string (dsdfgfg)","this is a (string (123456)",
"this is a (string) (FF4455GG)","this is a string (fdf","this is a string (dsdfgfg) temp",
"this is a string (dsdfgfg))"};
foreach (var s in strs)
{
Console.WriteLine(string.Format("{0}: {1}", s, rx.IsMatch(s).ToString()));
}
}
}
결과:
this is a string (dsdfgfg): True
this is a (string (123456): True
this is a (string) (FF4455GG): True
this is a string (fdf: False
this is a string (dsdfgfg) temp: False
this is a string (dsdfgfg)): False
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다