2014-01-07 6 views
0

단어 (사용하는 경우 다음의 양쪽에 공백이나 특수 문자가있을 수 있습니다) :세미 팬시 Regex.Replace() 함수

대시 (-), 슬래시 느낌표 점 (!), 줄임표 (... OR ...) (다른 점)

나는 일종의 수렁에 빠져있다. (나는), 콜론 (:), 마침표 (?), 물음표 이 수수께끼에서 나는 문자 그대로 내 검색에서 찾으려고 노력하고있는 모든 특수한 정규 표현식 문자 때문에. Regex.Escape를 사용할 수 있다고 믿습니다.이 경우에는 지금 당장은 작동하지 않습니다. 문자열을 시작

몇 가지 예는 다음과 같을 수 있습니다로 변경 :

private static string[] postCaps = { "-", "/", ":", "?", "!", "...", "…"}; 
private static string ReplacePostCaps(string strString) 
{ 

    foreach (string postCap in postCaps) 
    { 

     strString = Regex.Replace(strString, Regex.Escape(postCap), "/(?<=(" + Regex.Escape(postCap) + "))./", RegexOptions.IgnoreCase); 

    } 
    return strString; 

} 

가 대단히 감사합니다 :

여기
Change this: 
This is a dash - example 
To this: 
This is a dash - Example  <--capitalize "Example" with Regex 

This is another dash -example 
This is another dash -Example 

This is an ellipsis ... example 
This is an ellipsis ... Example 

This is another ellipsis …example 
This is another ellipsis …Example 

This is a slash/example 
This is a slash/Example 

This is a question mark ? example 
This is a question mark ? Example 

내가 지금까지 가지고있는 코드입니다!

+3

코드를 표시하십시오. – tnw

+0

대문자로 바꾸려면 대체 콜백이있는 Regex.Replace() 메서드 버전이 필요합니다. – nhahtdh

+0

이 튜토리얼을 통해 몇 시간을 보냅니다. http://www.regular-expressions.info/tutorial.html 무거운 곳이지만 정규식 엔진이 실제로 패턴을 사용하여 수행하는 작업에 대해 자세하게 설명합니다. – RobH

답변

4
당신은 단지 하나의 정규 표현식에서 문자 집합 추가 할 수 대신 문장의 목록을 반복 할 필요가 있지만, 안

:

(?:[/:?!…-]|\.\.\.)\s*([a-z]) 

Regex.Replace()와 함께 사용하려면 :

strString = Regex.Replace(
    strString, 
    @"(?:[/:?!…-]|\.\.\.)\s*([a-z])", 
    m => m.ToString().ToUpper() 
); 

정규식을 설명했다 :

(?:     # non-capture set 
    [/:?!…-]  # match any of these characters 
    | \.\.\.  # *or* match three `.` characters in a row 
) 
\s*     # allow any whitespace between matched character and letter 
([a-z])    # match, and capture, a single lowercase character 
1

어쩌면이게 효과가 있을지 :

var phrase = "This is another dash ... example"; 
var rx = new System.Text.RegularExpressions.Regex(@"(?<=[\-./:?!]) *\w"); 
var newString = rx.Replace(phrase, new System.Text.RegularExpressions.MatchEvaluator(m => m.Value.ToUpperInvariant()));