开发者

C# Replace wildcard

I am making an application to change the the resolution of that game, to the requested resolution.

StreamReader reader = new StreamReader(@"C:\Documents and Settings\ARTech\Bureaublad\PersistentSymbols.ini");//Reads the file.
string content = reader.ReadToEnd();//Puts the content of the file, into a variable.
reader.Close();
string replace = "persistent extern INDEX m_pixScreenWidth=(INDEX)" + txtWidth.Text + ";";
content = content.Replace("persistent extern INDEX m_pixScreenWidth=(INDEX)1920;", replace);//Replaces the ScreenWidth of the game to the requested number.
replace = "persistent extern INDEX m_pixScreenHeight=(INDEX)" + txtHeight.Text + ";";
content = content.Replace("persistent extern INDEX m_pixScreenHeight=(INDEX)1200;", replace);//Replaces the ScreenHeight of开发者_开发问答 the game to the requested number.

StreamWriter writer = new StreamWriter(@"C:\Documents and Settings\ARTech\Bureaublad\PersistentSymbols.ini");
writer.Write(content);//Saves the changes.
writer.Close();

The problem is, that the resolution is not always 1920 x 1200, so I need some kind of wildcard which accepts everything between persistent extern INDEX m_pixScreenWidth=(INDEX) and ;.


You should take a look to regexp :)

http://msdn.microsoft.com/en-us/library/system.text.regularexpressions.regex.aspx


You might want to look into an INI reader/writer such as this project: An INI file handling class using C#. Then you could grab the desired key and set the value appropriately.

Otherwise, you could write a regex such as this one:

string input = @"persistent extern INDEX m_pixScreenWidth=(INDEX)1920;
...
persistent extern INDEX m_pixScreenHeight=(INDEX)1200;";
string width = "800";
string height = "600";

string pattern = @"(persistent extern INDEX m_pixScreen(?<Type>Width|Height)=\(INDEX\))\d+;";
string result = Regex.Replace(input, pattern,
                    m => m.Groups[1].Value
                         + (m.Groups["Type"].Value == "Width" ? width : height)
                         + ";");

Console.WriteLine(result);

Pattern breakdown:

  • (persistent extern INDEX m_pixScreen(?<Type>Width|Height)=\(INDEX\)): Your expected text, including height/width and the index text, is placed in a capture group by the opening and closing parentheses. We will refer to this later.
  • (?<Type>Width|Height): a named capture group that alternates between width and height to capture both. This way one pattern can handle both types of text.
  • \(INDEX\): parentheses must be escaped to be matched literally since they hold special meaning in regex if unescaped (used for grouping as done above).
  • \d+: \d matches a digit [0-9]. The + makes it match at least one number (1 or more digits).
  • ;: this matches the trailing semicolon

A lambda is used with the MatchEvaluator overload of the Replace method. Essentially you are building the string back up. Groups[1] refers to the first group of text captured (see the first point above in the pattern breakdown). Next we check the named group of Type and check whether we are dealing with a width or a height. We substitute the new value appropriately. Finally we add that semicolon at the end to get the final replacement result.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜