2016-07-19 3 views
1

모든 문자열을 빼기 위해 <span class="notranslate">*any text*</span>의 문자열을 캡처하려고합니다. HTML이나 기타를 구문 분석 할 필요가 없습니다. 전체 섹션을 무시하면됩니다. 다른 태그를 유지하고 싶기 때문에 태그가 정확히 일치해야합니다. 주어진 문자열에는 최소한 하나의 태그가 있고 상한은 없습니다. (한 쌍 이상은 흔하지는 않지만)정규식을 사용하여 특정 (반복되는) 패턴을 제외한 모든 것을 캡처합니다.

내 궁극적 인 목표는 두 개의 텍스트를 매치하는 것입니다. 하나는 변수 이름이 있고 다른 하나는 변수 이름 값으로 대체되었습니다 (변수를 직접 대체 할 수는 없으며 해당 db에 액세스 할 수 없습니다). 이러한 변수는 내가 언급 한 범위 태그로 항상 둘러싸여 있습니다. 내 태그가 "번역하지 않는다"는 것을 알지만 - 이것은 사전 번역이므로 다른 모든 텍스트는 완전히 동일합니다. 예를 들어

이 내 두 개의 입력 텍스트 경우 :
Dear , I am sorry that you are having trouble logging in. Please follow the instructions at this URL and let me know if that fixes your problem.
또는
Dear <span class="notranslate"></span>, I am sorry that you are having trouble logging in. Please follow the instructions at this URL <span class="notranslate"></span> and let me know if that fixes your problem.:

Dear <span class="notranslate">$customer</span>, I am sorry that you are having trouble logging in. Please follow the instructions at this URL <span class="notranslate">$article431</span> and let me know if that fixes your problem.

Dear <span class="notranslate">John Doe</span>, I am sorry that you are having trouble logging in. Please follow the instructions at this URL <span class="notranslate">http://url.for.help/article</span> and let me know if that fixes your problem.

내가 정규식 반환 할이 둘 모두에 대해 String.Equals()를 쉽게 수행 할 수 있고 동등한 지 알아볼 수 있습니다. (변수를 대체하는 여러 텍스트에 대한 변수를 입력과 비교하여 일치하는 항목을 찾아야합니다.)

문자열에 "not notllate"가 있는지 여부를 쉽게 알 수있는 정규식이 있습니다. "섹션에 : (<span class="notranslate">(.+?)</span>), 내가 비교하기 전에 섹션을 제거해야할지 여부를 결정하는 방법입니다. 그러나 나는 (나는 매우 비슷한 생각으로) 위의 작업에 많은 어려움을 겪고있다.

나는 Expresso와 regexstorm.net을 테스트하기 위해 다른 SO 질문의 아이디어를 사용하여 (?:(.+?)(?:<span class=\"notranslate\">(?:.+?)</span>))의 다양한 변형을 시도해 보았습니다.하지만 모두 이해할 수없는 문제가 있습니다. 예를 들어, Expresso에서 거의 작동하는 것으로 보이지만 마지막 span 태그 세트 이후에 end 텍스트를 가져올 수는 없습니다. span 태그를 선택적으로 만들거나 끝에 다른 태그 (. +?)를 추가하려고하면 아무 것도 잡지 않습니다. 나는 lookaheads를 사용해 보았지만, 나중에 태그 + 내부 텍스트를 잡아 냈다.

+1

당신이 교체 작업을 수행 할 수 없습니다

string data = "Dear <span class=\"notranslate\">$customer</span>, I am sorry that you\r\n are havin" + "g trouble logging in. Please follow the instructions at this\r\n URL <span class=" + "\"notranslate\">$article431</span> and let me know if\r\n that fixes your problem."; string pattern = @"(?<Words>[^<]+)(?<Ignore><[^>]+>[^>]+>)?"; Regex.Matches(data, pattern) .OfType<Match>() .Select(mt => mt.Groups["Words"].Value) .Aggregate((sentance, words) => sentance + words); 

결과는 원래의 캐리지 리턴과 라인이있는 문자열이 실제로 예를 피드입니까? 첫 번째 정규식을 사용하고 각 일치 항목을 빈 문자열로 바꿉니다. – 4castle

+0

친애하는 신. 그래, 그럴거야. 나는 정규 표현식을 처음으로 사용하고 터널 비전을 개발하는 것에 대해 정신을 차렸다. 감사. – violaceous

+0

휴! 그것은 당신이 생각했던 것보다 쉽다는 것을 깨달을 때 항상 좋은 느낌입니다. 기꺼이 도와 드리겠습니다 :) – 4castle

답변

0

이것은 모두 캡처 한 다음 무시되는 일치하는 HTML 태그를 처리합니다.

Dear , I am sorry that you 
    are having trouble logging in. Please follow the instructions at this 
    URL and let me know if 
    that fixes your problem. 
관련 문제