액세스 DB에서 쿼리를 읽고 쿼리의 모든 키워드를 SQL 실행 가능한 쿼리로 바꾸도록 쿼리를 수정해야하는 요구 사항이 있습니다. 예 : 'Select key from table1'쿼리가 있다고 가정합니다.
여기서 "key"는 예약 된 키워드입니다. "Select [key] from table1"(sql에서 작동 함)로 수정해야합니다.
쿼리에 여러 키워드가있을 수 있으므로 아래와 같은 정규식이 있습니다.
string pattern = "Key|IN|ON|VIEW]";
Regex rgx = new Regex(pattern);
패턴을 확인하는 아래 코드가 있습니다 ..
string pattern = "Key|IN|ON|VIEW]";
Regex rgx = new Regex(pattern);
string strModifiedQuery = string.Empty;
strModifiedQuery = strQueryText;
foreach (Match m in rgx.Matches(pattern))
{
Console.WriteLine(m.Value);
if (m.Value == "Key")
{
strModifiedQuery = rgx.Replace(strModifiedQuery, "[Key]");
}
if (m.Value == "IN")
{
strModifiedQuery = rgx.Replace(strModifiedQuery, "[IN]");
}
if (m.Value == "ON")
{
strModifiedQuery = rgx.Replace(strModifiedQuery, "[IN]");
}
C #에서 일치하는 패턴을 적절한 값으로 바꾸는 더 좋은 방법이 있습니까?
var pattern = @"(?i)Key|IN|ON|VIEW";
var subject = "Select key from table1";
var result = Regex.Replace(subject, pattern, @"[$0]");
생산
Select [key] from table1
(?i)
대소 문자 무시를 켜고 패턴 바꾸기 [$0]
는 대괄호로 묶인 일치 항목을 대체합니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다