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();
}
Related
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 1 year ago.
Improve this question
I got a task for URL validation in which url should not contain any special characters and it should accept only https. I tried with
^((https)://)?([\w+?\.\w+])+([a-zA-Z0-9\\\/\.\:]*)?$
this regex pattern it works fine for special characters but not working for https, Its accepting http also whereas i need it to accept only https. I googled it but unable to find any solution.
Is regex actually a requirement ?
Actually, it's far more simple (and I guess, efficient) to test for the first 7 characters :
var tests = new String[]{
"http://shouldfail",
"https://shouldsucceed",
"ftp://fail"
};
foreach(var test in tests){
if(test.StartsWith("https://", StringComparison.CurrentCultureIgnoreCase)){
Console.WriteLine(test + " is OK");
}else{
Console.WriteLine(test + " is not OK");
}
}
You could do something like this for example:
static void Main(string[] args)
{
string pattern = #"^(https:\/\/)[\w.-]+(?:\.[\w\.-]+)+[\w\-\._~:/?#[\]#!\$&'\(\)\*\+,;=.]+$";
Regex reg = new Regex(pattern);
string[] urls = { "https://test.com", "http://test.com" };
foreach (var item in urls)
{
var test = reg.IsMatch(item);
Console.WriteLine(test.ToString());
}
Console.ReadKey();
}
Result is:
True
False
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 2 years ago.
Improve this question
write a regular expression for taxi numbers in Belarus
1)region code (a digit from 1 to 7);
2)three uppercase Latin letters. For the `7' region (Minsk), the combinations TAX, TBX, TEX are used, for other regions, only TAX, TBX are currently in use;
3)a single space;
4)four-digit number from 1 to 9999, with leading zeroes.
You almost got it. Try something like
using System;
using System.Text.RegularExpressions;
public class Program
{
public static void Main()
{
string text = "1TAX 1234\r\n7TEX 9876\r\n5TEX 9876\r\n4TBX 9876";
string pattern = #"^(?:(?:[1-6]T[AB]X)|(?:7T[ABE]X))\s\d{4}";
Regex r = new Regex(pattern, RegexOptions.Multiline);
foreach (var m in r.Matches(text))
{
Console.WriteLine("> {0}", m.ToString());
}
}
}
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
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
Hi I am facing problem to get specific string. The string as below:
string myPurseBalance = "Purse Balance: +0000004000 556080";
I want to get 4000 out only.
if your string format/pattern is fix then you can get specific value
string myPurseBalance = "Purse Balance: +0000004000 556080";
//
var newPursebal =Convert.ToDouble(myPurseBalance.Split('+')[1].Split(' ')[0]);
You can use this regular expression:
string extract = Regex.Replace(myPurseBalance, #"(.*?)\: \+[0]*(?<val>\d+) (.*)", "${val}")
It searches for the decimals after :, trims the leading 0s and removes everything after the last whitespace.
You can use string.Split to get the +0000004000 and then use string.Substring to get the last four characters by passing the Length-4 as starting index.
string str = myPurseBalance.Split(' ')[2];
str = str.Substring(str.Length-4);
Learn Regex . Here is just simple tutorial
using System;
using System.Text.RegularExpressions;
namespace regex
{
class MainClass
{
public static void Main (string[] args)
{
string input = "Purse Balance: +0000504000 556080";
// Here we call Regex.Match.
Match match = Regex.Match(input, #"\+[0]*(\d+)",
RegexOptions.IgnoreCase);
// Here we check the Match instance.
if (match.Success)
{
// Finally, we get the Group value and display it.
string key = match.Groups[1].Value;
Console.WriteLine(key);
}
}
}
}
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.