write regex c # [closed] - c#

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());
}
}
}

Related

Accept only https in URL regex pattern c# [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 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

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();
}

Extracting Address' from police reports [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 7 years ago.
Improve this question
I'm currently working on a program that would be searching through medium-large strings and pulling out addresses so they can then be geocoded.
An example of what I had was:
private void cardCheck()
{
cardCount = mobjEntity.CardCount;
for (int i = 0; i < cardCount; i++)
{
card = mobjEntity.Card[i];
if (card.Text.Contains(" STREET ") ||
card.Text.Contains(" Street") ||
card.Text.Contains(" street") ||
card.Text.Contains(" ST ") ||
card.Text.Contains(" St ") ||
card.Text.Contains(" st "))
{
}
}
}
I'm not very good at regex and I was hoping one of you regex wiz' could show me a useful link for testing/learning regex.
I have written what I have above for every street type and it's very tedious and I still don't even know what to do from there.
This is an Ideal Input Output:
Input:
On Friday, April 9, 2010, at 9:45 a.m., I, Officer Janice Ruiz, was dispatched to 2170 Powell Street to investigate a burglary. I met with Frank Gaines, the homeowner who had reported the burglary.
Output:
2170 Powell Street
Have you tried using a regular expression to search the text? A quick Google search returns several RE's that may work for you. Here is one example:
\d{1,3}.?\d{0,3}\s[a-zA-Z]{2,30}\s[a-zA-Z]{2,15}
Here is a proof of concept: https://regex101.com/r/dH3jJ8/1

c# how to get specific string [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
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);
}
}
}
}

Regular expressions : how to find [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 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.

Categories