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.
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 debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I have this FINAL PAYMENT $25 string on a MVC c# application.
I want to split into FINAL PAYMENT and 25
I tried doing this
string s = "FINAL PAYMENT $25";
string[] str1 = s.Split('$');
//result: 25
How can I get the rest. Can anyone help?
Split method returns a string array, if you need both elements of this array, Try:
string s = "FINAL PAYMENT $25";
string[] resArray = s.Split('$');
var FPayment = resArray[0];
var second25= resArray[1];
You can use indexOf instead of a Split
string s = "FINAL PAYMENT $25";
int index = s.IndexOf("$");
String final_pay = s.Substring(index + 1);
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
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();
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
I would like to split directory into two parts:
For example
//Hello//Products//App//Images//Room//40//Tulips.jpg
into
//Hello//Products//App
and
//Images//Room..40//Tulips.jpg
var splitOn = "App";
var path = "//Hello//Products//App//Images//Room//40//Tulips.jpg";
var parts = path.Split(new string[] { splitOn }, StringSplitOptions.None);
Console.WriteLine(parts[0] + splitOn);
Console.WriteLine(parts[1]);
In order to split by a word (or in this case folder) you need to wrap the term in a string array before passing it to the String.Split function. Splitting on "App" will also remove "App" from the result, so we concatenate it again before we write it to the console.
First split the string based on the double forward slash and then assign to array.
string path= Hello//Products//App//Images//Room//40//Tulips.jpg
string[] names = path.Split('//');
After this collect the words like this:-
string first_part=names[0] + "//" + names[1];