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

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

Split string data Using Regex in 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 6 years ago.
Improve this question
Hi I have String Like Following
string str=SCAN: SITE: http://www.vividinfotech.com SCAN: DOMAIN: www.vividinfotech.com SCAN: IP: 66.55.155.156 SYSTEM: NOTICE: Running on: ApacheRECOMMENDATIONS: 0: Security Header: X-XSS-Protection MissingRECOMMENDATIONS: 0: We did not find the recommended security header forXSS Protection on your site.
I need split the like SCAN and RECOMMENDATIONS datas
as follows :
SCAN:
1.http://www.vividinfotech.com
2.DOMAIN: www.vividinfotech.com
3.IP: 66.55.155.156
is there any way do this
Simple way to do this is using the following regular expression:
string str = "SCAN: SITE: http://www.vividinfotech.com SCAN: DOMAIN: www.vividinfotech.com SCAN: IP: 66.55.155.156 SYSTEM: NOTICE: Running on: ApacheRECOMMENDATIONS: 0: Security Header: X-XSS-Protection MissingRECOMMENDATIONS: 0: We did not find the recommended security header forXSS Protection on your site.";
Match m = Regex.Match(str, #"SCAN: SITE: (.*)SCAN: (DOMAIN:.*)SCAN: (IP: [\d\.]*)");
if (m.Success && m.Groups.Count == 4)
{
string site = m.Groups[1].Value;
string domain = m.Groups[2].Value;
string ip = m.Groups[3].Value;
}
If you wany a shorter way, you can use linq instead of regex. Something like:
string[] resultsSCAN = str.Split(new string[] { "SCAN: " }, StringSplitOptions.None).Skip(1).Take(3).ToArray();
resultsSCAN[resultsSCAN.Count() - 1] = resultsSCAN.Last().Split(new string[] { "SYSTEM: " }, StringSplitOptions.None).First();
I think it's what you want.

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

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

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