'#if DEBUG'및 '#endif'가 두 마크 사이의 콘텐츠를 제거하는 방법
예 : SQL 문자열 아래에서는 '#if DEBUG'및 '#endif' /**if DEBUG**/
와 같이와 사이의 문자열을 제거 할 것으로 예상합니다./**endif**/
select
id
,name
/**if DEBUG**/
,test1
,test2
/**endif**/
from table
where sDate >= '2021-01-01'
/**if DEBUG**/
and test1 = '123456'
/**endif**/
예상 결과 :
select
id
,name
from table
where sDate >= '2021-01-01'
정규식을 사용해 보았습니다.
(\/\*\*if\sDEBUG\*\*\/)(?<content>)(\/\*\*endif\*\*\/)
(\/\*\*if\sDEBUG\*\*\/)|(\/\/[\w\s\']*)|(\/\*\*endif\*\*\/)
문자열을 가져 오지만 작동하지 않으려면 내 코드 :
void Main()
{
var input = @"
select
id
,name
/**if DEBUG**/
,test1
,test2
/**endif**/
from table
where sDate >= '2021-01-01'
/**if DEBUG**/
and test1 = '123456'
/**endif**/
";
var matchs = Regex.Matches(input,@"(\/\*\*if\sDEBUG\*\*\/)(?<content>)(\/\*\*endif\*\*\/)")
.Select(m=>new {Key = m.Groups["content"].Value,Value=m.Groups["content"].Value})
;
foreach (var m in matchs)
{
input.Replace(m.Value,"");
}
Console.WriteLine(input);
}
Regex.Replace
직접 사용하지 않는 이유는 무엇 입니까? 줄 바꿈 을 일치시키는 데 .*?
사용하는 한 디버그 마커 사이의 텍스트를 캡처하는 데 사용할 수 있습니다 .RegexOptions.Singleline
.
void Main()
{
var input = @"
select
id
,name
/**if DEBUG**/
,test1
,test2
/**endif**/
from table
where sDate >= '2021-01-01'
/**if DEBUG**/
and test1 = '123456'
/**endif**/
";
input = Regex.Replace(input, @"/\*\*if\sDEBUG\*\*/.*?/\*\*endif\*\*/", "", RegexOptions.Singleline);
Console.WriteLine(input);
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다