Regular expressions : how to find [closed] - c#

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
Why doesn't this regex, constructed like this:
tmprect = string "gg_rct_MyReg1"
regex = #"^\s*set\s+"+tmprect+#"\s*=\s*Rect\s*\(\s*(.+)\s*,\s*(.+)\s*,\s*(.+)\s*,\s**(.+)\s*\).*$";
not work for
set gg_rct_MyReg1 = Rect (-704.0 , -352.0, 224.0 , 448.0) //rect 1
What did I do wrong?
///edited:
string findrectcoord = #"^\s*set\s+" + tmprect + #"\s*=\s*Rect\s*\(\s*([^,\s]*)\s*,\s*([^,\s]*)\s*,\s*([^,\s]*)\s*,\s*([^)\s]*)\s*\).*$";
StreamReader file3 = new StreamReader(openFileDialog1.FileName);
string line2;
while ((line2 = file3.ReadLine()) != null)
{
Regex foundrectr = new Regex(findrectcoord);
Match foundrectm = foundrectr.Match(line2);
if (foundrectm.Success)
{
MessageBox.Show("YES");
}
}
string:
set gg_rct_MyReg1 = Rect( -704.0 , -352.0, 224.0 , 288.0 ) //JassCode
Not Found

The regex itself, while ugly and inefficient, should work. You do need to assign the string you're adding into the regex before building the regex, though. While we're at it, let's clean it up:
string tmprect = "gg_rct_MyReg1";
Regex regexObj = new Regex(#"^\s*set\s+" + tmprect +
#"\s*=\s*Rect\s*\(\s*([^,\s]*)\s*,\s*([^,\s]*)\s*,\s*([^,\s]*)\s*,\s*([^)\s]*)\s*\).*$");
([^,\s]*) matches any number of characters except commas or spaces. That is more specific than .* which will match too much and force the regex engine to backtrack.

Related

Matching two strings together with Substituted Characters [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
Imagine a gamble game where you have to match all three words to get a reward.
So if you get HHH you get an award.
I want to be able to subsitute W to mimick any letter.
For example:
HWW = HHH
and
HHW = HHH
but
WWW = WWW
How do i go about doing this?
Before matching a string to a pattern, change the wild card characters to a character that matches to anything. Like this:
// this method change W wild-card character to a character that match anything
private static bool MatchToString(string stringToMatch, string pattern) {
Regex r = new Regex(pattern.Replace("W", "."));
return r.Match(stringToMatch).Success;
}
static void Main() {
// these are matches
Console.WriteLine(MatchToString("HHH", "HWW"));
Console.WriteLine(MatchToString("HHH", "HHW"));
Console.WriteLine(MatchToString("WWW", "WWW"));
Console.WriteLine(MatchToString("HHWH", "WWWW"));
//these are doesn't
Console.WriteLine(MatchToString("HHH", "HHK"));
Console.WriteLine(MatchToString("HHH", "HKK"));
Console.WriteLine(MatchToString("WWW", "ABC"));
Console.ReadLine();
}

Changing Host Names dynamically [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have files on a local server with the address of \\localServerAddress\Folder\Program.exe. I need to remove the server address dynamically and replace it with a different server address that is being selected elsewhere in the form. The server names can be different lengths, therefore, I can not use the string.Substring function.
So given the input
\\localServerAddress\Folder\Program.exe
I would like the result
\\differentServerAddress\Folder\Program.exe
If you are always working with UNCs
Then
string toRemove = new Uri(yourString).host;
string newString = yourString.Replace(String.format(#"\\{0})",toRemove)
, String.format(#"\\{0})",whateveryouwant));
Use this method:
string changeServerInPathString(string originalString, string newServer)
{
List<string> stringParts = originalString.TrimStart('\\').Split('\\').ToList();
stringParts.RemoveAt(0);
stringParts.Insert(0, newServer);
return string.Join("\\", stringParts.ToArray()).Insert(0, "\\\\");
}
You can use something like this:
void Main()
{
string path = #"\\localServerAddress\Folder\Program.exe";
UriBuilder bld = new UriBuilder(path);
bld.Host = "NewServer";
Console.WriteLine(bld.Uri.LocalPath);
}
Result: \\newserver\Folder\Program.exe
string text = #"\\test\FolderName\foo.exe";
text = text.Replace('\\', '-'); \\ this is done as I was not able to make the regex **\\\\\\(.)*?\\** , work.
Regex rg = new Regex("--.*?-"); \\ if in case the above mentioned regex is made to work correctly please replace the regex with the same.
text = rg.Replace(text, "");
Console.WriteLine(text.Replace('-', '\\'));
Console.Read();

How to format a very long text on a TextBox? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
How to format my text in TextBox?
My text value is:
00010001008002020100010000530997000014820000148200010000012C00001482000014820000148200010000012C000014820000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000101000000000000000000000000000000000000003F
And I want my output to be like this:
00010001-0080-02020100010000-53099700-00148200-00-14820001-0000-012C0000-14820000-1482000-0148-20001000-0012C000-0148-20000000-00000000-0000-00-000000000-00000000-0000-00000000-0000000-0000-00000000-00000000-00000000-0000-00000000-00-00000-000001010-00000000-000000000-0000-000000000-00000003-F
Your format must be fixed. It should not be dynamic.
I am just providing a logic you could append the further detail in regex string.
string mystring = "000100010080"
string regex = #"(\w{4})(\w{4})(\w{4})";
string strValue = Regex.Replace(mystring, regex, #"$1-$2-$3");
OUTPUT:
0001-0001-0080
EDITED: Take a look at complete example
string[] patern = "XXXXXXXX-XXXX-XXXXXXXXXXXXXX-XXXXXXXX-XXXXXXXX-XX-XXXXXXXX-
XXXX-XXXXXXXX-XXXXXXXX-XXXXXXX-XXXX-XXXXXXXX-XXXXXXXX-XXXX-
XXXXXXXX-XXXXXXXX-XXXX-XX-XXXXXXXXX-XXXXXXXX-XXXX-XXXXXXXX-
XXXXXXX-XXXX-XXXXXXXX-XXXXXXXX-XXXXXXXX-XXXX-XXXXXXXX-XX-
XXXXX-XXXXXXXXX-XXXXXXXX-XXXXXXXXX-XXXX-XXXXXXXXX-XXXXXXXX-
X".Split('-');
string mystring = "00010001008002020100010000530997000014820000148200010000012C0
0001482000014820000148200010000012C00001482000000000000000000
0000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000001010000000000000000000000000000
00000000003F";
string regex = string.Empty;
string match = string.Empty;
for(int i=0; i<patern.Length;i++)
{
regex += #"(\w{" + patern[i].Length + "})";
match += "$" + (i + 1).ToString() + "-";
}
match = match.Substring(0, match.Length - 1);
txtMyTextBox.Text = Regex.Replace(mystring, regex, match);
Assuming that you have a fixed pattern of inserting hyphens and the string length is same every time, then you could do something like this:
int[] indices = new int[] { 2, 5, 11 };
string yourLongString = "blahblahblah";
foreach( var index in indices.Reverse() )
{
yourLongString = yourLongString.Insert( index - 1, "-" );
}
OR
Assuming you have no predefined pattern, you could insert hyphens anywhere and hence still you could use the same code above with the tweak to randomize the indices array, if needed.

Need to a 'like' comparision [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Need help to find C# code to do a SQL-type like search (case insensitive)
could you please help me with regex code for this. Pattern and test candidates are both user input
* could be anywhere. so pattern could be .T*S.com
e.g.
Pattern = *.test.com
Test Candidate1 = abc.test.com Result = Pass
Test Candidate2 = abc.tESt.com Result = Pass
Test Candidate3 = abc.itest.com Result = FAIL
In case the * is at the front you can use String.EndsWith().
Like
"abc.test.com"
.EndsWidth(".test.com", StringComparison.InvariantCultureIgnoreCase);
returns true.
If you don't want to go the regex route and the candidate always ends with .test.com you can get rid of the * in your pattern and then check with EndsWith:
if (candidate.EndsWith(pattern, StringComparison.InvariantCultureIgnoreCase))
// you have a match
If you want the regex, the pattern is need to be:
public bool IsMatch(string s,string pattern)
{
return System.Text.RegularExpressions.Regex.IsMatch(s, pattern, System.Text.RegularExpressions.RegexOptions.IgnoreCase);
}
string pattern = ".*\.test\.com"
Console.WriteLine(IsMatch("abc.test.com",pattern).ToString()); //PASS
Console.WriteLine(IsMatch("abc.tESt.com",pattern).ToString()); //PASS
Console.WriteLine(IsMatch("abc.itest.com",pattern).ToString()); //FAIL

how can i remove "/ ONLY" from "Rs1500/ONLY",using c# [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
string oldstring = textBox9.Text;
string newstring = oldstring.Remove(0, 2);
string o = newstring.Remove(4, 7);
Now, I want to get only "1500",rest of the things to be removed.
How can I do this? Please help me.
Try the following code
string newstring = Regex.Replace(oldstring, #"[^\d]", "");
It shall work.
there are number of ways to do this.. you can use Split() as below:
string oldstring = "Rs1500/ONLY";
string[] newstring = oldstring.Split('/');
string o = newstring[0];
This will give you "RS 1500" as result.
And if you want to remove RS as well then just add the below code in last of the above code:
string final = newstring[0].ToString().Replace("Rs","");
That's All
You can use Replace
string oldstring = textBox9.Text;
string newstring = oldstring.Replace("Rs","").Replace("/ONLY","");
This should give you only 1500
Try this way
string ss = "Rs1500/ONLY";
string[] newss = ss.Split('/');
ss = newss[0].ToString().Replace("Rs","");//Or try string.Empty() for instead of ""
See this demo code : with output

Categories