2010-04-26 3 views
2

은 내가 특별히, 텍스트 블록을 특정 텍스트 (이름)를 위해 그것을 스캔하여 URL로 돌려하려고 : 텍스트의동일한 텍스트가있는 oldValues로 문자열을 두 번 바꾸는 방법은 무엇입니까?

블록 : 이제 내가 무엇을

Chairman Joe Smith has announced a new plan to decrease expenditures by outsourcing the planning of the new dining hall. Smith states the current project managers do not have excess time to commit to this new project and this will be less costly than hiring a new or contract project manager.

을 조 스미스 회장 또는 조 스미스 또는 스미스 회장 또는 스미스 회장의 사례를 취하여 프로필/바이오에 링크하십시오. 내가 알고있는 모든 문자열 메서드를 사용하여 (문자열 바꾸기, 문자열 작성기, 일치하는 문자열 앞뒤에 텍스트 추가) Smith와 다른 이름을 검색 할 때 문제가 발생합니다.

나는 다음 시도 할 경우 :

String.replace("Smith", "<a href='smithbio.html'>Smith</a>") 
String.replace("Chairman Joe Smith", "<a href='smithbio.html'>Chairman Joe Smith</a>") 

어디 스미스는 그의 이름 실패 만 스미스가 링크 될 것의 일부로서 사용되기 때문에 그것은 나쁜 것입니다.

하지만 반대하려고하면이 뜻이 내포 된 링크를 생성

String.replace("Chairman Joe Smith", "<a href='smithbio.html'>Chairman Joe Smith</a>") 
String.replace("Smith", "<a href='smithbio.html'>Smith</a>") 

합니다.

아마 내가 정규 표현식 검사와 함께 regex.Replace를 사용해야한다고 생각합니다. 그러나 그렇게한다면 나는 그것을하는 방법을 생각해 내는데 어려움을 겪고있다. 어떻게 이러한 여러 대체 할 수 있지만 다른 string.replacer 호출되는 않는 한이 문자열을 바꿀 말합니까? 참고로 다음과 같이

답변

3

당신은 정규식을 사용한다 ... 그것은 여기에 중요하지만 경우를 생각, VB에서 이것을하지 않는 수행 : (테스트 VB)

Regex.Replace(str, "(Chairman\s+)?(Joe\s+)?Smith", _ 
    "<a href='smithbio.html'>$0</a>") 

$0 몇 가지 중 하나입니다 expressions that can be included in the replacement string.

런타임에만 이름을 알고있는 경우 Regex.Escape으로 전화해야합니다.

+0

감사 :

다음은 예입니다! 이 예제와 링크는 매우 유용했습니다. 회장 조 스미스 (Joe Smith)가 여러 제목을 갖고 있는데, 내가 언급 한 내용을 파싱하고 싶다면 다음과 같이 말하십시오. "((Honored \ s +) | (Chairman \ s +) | (Joe \ s +) "? Smith"'? – SventoryMang

+1

@ sah302 : 괄호 안의 모든 묶음을 괄호로 감싸면'|'는 너무 욕심이납니다. "(Honored \ s + | Title2 \ s + | Chairman \ s +)? (Joe \ s +)? Smith "(테스트 됨) – SLaks

1

.NET regex 개체로 수행 할 수있는 작업 중 하나는 일치 항목을 Regex.Replace에 전달 된 결과로 바꾸는 것입니다.

대리인의 경우 대리자에서 반환 된 대체 텍스트를 결정할 때 일치하는 결과 (원하는 주변 문자열)를 사용할 수 있습니다.

0

나는 이것을하는 것이 좋지만, 프로그래머가 제시 한 문제에서 알고리즘을 추론하고 추론 할 수 있어야합니다. 특히 레거시 코드 기반을 유지할 때 특히 그렇습니다. 우리는 높은 수준의 추상화를 모두 망쳐 놓았습니다. 우리는 X, Y, Z 및 붐을 어떻게 수행 할 수 있는지 묻습니다. RegEx 또는 LINQ 쿼리를 던집니다. 나는 그것들이 나쁜 것들이라고 말하는 것이 아니라 조금씩 더 생각하는 동안 지불한다. 아마도 이것이 코드 골프 또는 그 이상의 것을 의미하지만, OP가 알고리즘을 통해 추론 된 경우, 통역 방식을 제시하는 것이 훨씬 좋아질 것입니다. 영업 담당자는 아마도 정규식이 해결책.스미스 스미스

  • 조 스미스
  • 스미스
  • 회장을

    1. 회장 조 : 기록하고 발견 된 문자열의 인덱스 및 길이의 목록을 관리 할 수있는 정규식을 사용하지 않고

    그런 다음 해당 목록으로 이동하여 각 항목을 해당 링크로 바꿉니다.

    이 알고리즘을 향상시킬 수 있다고 확신합니다.

    class Instance 
    { 
        public int start; 
        public int length; 
        public string text; 
        public Instance(int _start, int _length, string _text) 
        { 
         start = _start; 
         length = _length; 
         text = _text; 
        } 
    }; 
    
    static void Main(string[] args) 
    { 
        string test = "Chairman Joe Smith has announced a new plan to decrease expenditures by outsourcing the planning of the new dining hall. Smith states the current project managers do not have excess time to commit to this new project and this will be less costly than hiring a new or contract project manager."; 
        string[] lookup = { "Chairman Joe Smith", "Joe Smith", "Smith", "Chairman Smith" }; 
        List<Instance> li = new List<Instance>(); 
    
        // record each instance of specified strings 
        foreach (string name in lookup) 
        { 
         int index = 0; 
         do 
         { 
          index = test.IndexOf(name, index); 
          if (index > -1) 
          { 
           li.Add(new Instance(index, name.Length, name)); 
           index += name.Length; 
          } 
         } while (index > -1); 
        } 
    
        // eliminate duplicate instances 
        Retry: 
        foreach (Instance i in li) 
        { 
         foreach (Instance j in li) 
         { 
          if (j != i) 
          { 
           if ((j.start >= i.start) && (j.start + j.length <= i.start + i.length)) 
           { 
            li.Remove(j); 
            goto Retry; 
           } 
          } 
         } 
        } 
    
        // replace each instance with respective text 
        foreach (Instance i in li) 
        { 
         test = test.Remove(i.start, i.length); 
         string final = "<a href='smithbio.html'>" + i.text + "</a>"; 
         test = test.Insert(i.start, final); 
         foreach (Instance j in li) 
         { 
          j.start += (final.Length - i.length); 
         } 
        } 
    
        Console.WriteLine(test); 
        Console.ReadLine(); 
    } 
    
  • 관련 문제