이 내용에 대해서 검색하려고 영어로 하는데... 약간의 애로사항이 있었다.
검색어는 이렇게 했다;.

regular expression parameter by quotes

그렇게 해서 찾은 글이 https://stackoverflow.com/questions/171480/regex-grabbing-values-between-quotation-marks 이였고, 이 글을 통해서 답은 얻었다.

정규 표현식은 아래와 같다.

\"(.*?)\"

즉 (.*?) 가 핵심이다.

위의 내용 처럼 Argument 값을 정규식에 대입하려면 Argument 전체 값이 필요한데, Main(string [] args)를 통해서 받은 args 값은 공백으로 전부 짤라놔서, 저 정규식을 대입해봐야 아무 도움이 안된다.
즉 자르기 전, 원본 Arguments 값을 얻어와야 한다.

string[] aryArgs = Environment.GetCommandLineArgs();            
string sArgOnly = Environment.CommandLine.Replace("\"" + aryArgs[0] + "\""  , "");

이제.. 저렇게 얻은 값을 " "  으로 꺼내려면 아래와 같이 코드를 짜면 추출할 수 있다.

Regex regex = new Regex("\"(?<arg>.*?)\"");

List<string> aryAllArgs = new List<string>();
MatchCollection matches = regex.Matches(sArgOnly);
foreach(Match match in matches)
{
    aryAllArgs.Add(match.Groups["arg"].Value);
}
728x90

+ Recent posts