How to get Last Index Of '\' or '//', whichever comes last? - c#

I want to get lastindexof character from url which comes from the database on the basis of '\' or '//'
Say for example i have string like this
Administration\Masters\EmployeePulseDetailsMaster.aspx
Administration/Masters/SearchKnowYourCollegues.aspx
Administration//SMS//PushSMS.aspx
I am using that code
foreach (var item in SessionClass.UserDetails.SubModules)
{
if (Request.RawUrl.Contains(item.PageURL.Substring(item.PageURL.LastIndexOf('\\') + 1))
|| Request.RawUrl.Contains(item.PageURL.Substring(item.PageURL.LastIndexOf('/') + 1)))
{
Response.RedirectPermanent("~/Login.aspx");
}
}

You can use a regular expression to find the last occurrence of any character in a group by constructing a regular expression that looks like this:
[target-group][^target-group]*$
In your case, the target group is [/\\], so the search would look like this:
var match = Regex.Match(s, #"[/\\][^/\\]*$");
Here is a running example:
var data = new[] {
#"quick/brown/fox"
, #"jumps\over\the\lazy\dog"
, #"Administration\Masters\EmployeePulseDetailsMaster.aspx"
, #"Administration/Masters/SearchKnowYourCollegues.aspx"
, #"Administration//SMS//PushSMS.aspx"
};
foreach (var s in data) {
var m = Regex.Match(s, #"[/\\][^/\\]*$");
if (m.Success) {
Console.WriteLine(s.Substring(m.Index+1));
}
}
This prints
fox
dog
EmployeePulseDetailsMaster.aspx
SearchKnowYourCollegues.aspx
PushSMS.aspx
Demo.

I guess you want to determine if the name of the current page is in the list of SessionClass.UserDetails.SubModules. Then i'd use Request.Url.Segments.Last() to get only the name of the current page(f.e. PushSMS.aspx) and System.IO.Path.GetFileName to get the name of each url. GetFileName works with / or \:
string pageName = Request.Url.Segments.Last();
bool anyMatch = SessionClass.UserDetails.SubModules
.Any(module => pageName == System.IO.Path.GetFileName(module.PageURL));
if(anyMatch) Response.RedirectPermanent("~/Login.aspx");
You need to add using System.Linq; for Enumerable.Any.

Related

Check if a particular string is contained in a list of strings

I'm trying to search a string to see if it contains any strings from a list,
var s = driver.FindElement(By.Id("list"));
var innerHtml = s.GetAttribute("innerHTML");
innerHtml is the string I want to search for a list of strings provided by me, example
var list = new List<string> { "One", "Two", "Three" };
so if say innerHtml contains "One" output Match: One
You can do this in the following way:
int result = list.IndexOf(innerHTML);
It will return the index of the item with which there is a match, else if not found it would return -1.
If you want a string output, as mentioned in the question, you may do something like:
if (result != -1)
Console.WriteLine(list[result] + " matched.");
else
Console.WriteLine("No match found");
Another simple way to do this is:
string matchedElement = list.Find(x => x.Equals(innerHTML));
This would return the matched element if there is a match, otherwise it would return a null.
See docs for more details.
You can do it with LINQ by applying Contains to innerHtml for each of the items on the list:
var matches = list.Where(item => innerHtml.Contains(item)).ToList();
Variable matches would contain a subset of strings from the list which are matched inside innerHtml.
Note: This approach does not match at word boundaries, which means that you would find a match of "One" when innerHtml contains "Onerous".
foreach(var str in list)
{
if (innerHtml.Contains(str))
{
// match found, do your stuff.
}
}
String.Contains documentation
For those who want to serach Arrray of chars in another list of strings
List WildCard = new() { "", "%", "?" };
List PlateNo = new() { "13eer", "rt4444", "45566" };
if (WildCard.Any(x => PlateNo.Any(y => y.Contains(x))))
Console.WriteLine("Plate has wildchar}");

Replace single group via RegEx in all matches

I have a text containing HTML-Elements, where hyperlinks contain not URLs but IDs to the item the hyperlink should open. Now i'm trying to get all those IDs and replace them with new IDs. The scenario is, that all ID's have changed and i have a dictionary with "oldId -> newID" and need to replace that in the text.
This input
Some text some text <a href = "##1234"> stuff stuff stuff <a href="##9999"> xxxx
With this Dictionary mapping
1234 -> 100025
9999 -> 100026
Should generate this output
Some text some text <a href = "##100025"> stuff stuff stuff <a href="##100026"> xxxx
So far i have this:
var textContent = "...";
var regex = new Regex(#"<\s*a\s+href\s*=\s*""##(?<RefId>\d+)""\s*\\?\s*>");
var matches = regex.Matches(textContent);
foreach (var match in matches.Cast<Match>())
{
var id = -1;
if (Int32.TryParse(match.Groups["RefId"].Value, out id))
{
int newId;
// idDictionary contains the mapping from old id to new id
if (idDictionary.TryGetValue(id, out newId))
{
// Now replace the id of the current match with the new id
}
}
}`
How do i replace the IDs now?
Don't parse HTML with regular expressions.
But if you must, if you're trying to perform a replacement, use the Replace method.
var updatedContent = regex.Replace(textContent, match =>
{
var id = -1;
if (Int32.TryParse(match.Groups["RefId"].Value, out id))
{
int newId;
// idDictionary contains the mapping from old id to new id
if (idDictionary.TryGetValue(id, out newId))
{
// Now replace the id of the current match with the new id
return newId.ToString();
}
}
// No change
return match.Value;
});
Edit: As you've pointed out, this replaces the entire match. Whoops.
Firstly, change your regular expression so the thing you'll be replacing is the entire match:
#"(?<=<\s*a\s+href\s*=\s*""##)(?<RefId>\d+)(?=""\s*\\?\s*>)"
This matches just a string of digits, but ensures it has the HTML tag before and after it.
It should now do what you want, but for tidiness you can replace (?<RefId>\d+) with just \d+ (as you don't need the group any more) and match.Groups["RefId"].Value with just match.Value.
Just use callback in replace.
regex.Replace(textContent, delegate(Match m) {
int id = -1, newId;
if (Int32.TryParse(m.Groups["RefId"].Value, out id)) {
if (idDictionary.TryGetValue(id, out newId))
return newId.ToString();
}
return m.Value; // if TryGetValue fails, return the match
});
Unless you are pulling the new IDs from the HTML aswell, I don't see why you can't just use a direct String.Replace here
var html = "Some text some text <a href = '##1234'> stuff stuff stuff <a href='##9999'> xxxx";
var mappings = new Dictionary<string, string>()
{
{ "1234", "100025" },
{ "9999", "100026" },
...
};
foreach (var map in mappings)
{
html = html.Replace("##" + map.Key, "##" + map.Value);
}
Fiddle

Regex expression to search upto nested level

How to search search string upto nested level using Regex expression
Like say: I have string like
var str = "samir patel {samirpatel#test1.com{sam#somedomain.com}}";
Out put should be sam#somedomain.com
You could simply use this pattern:
{([^{}]*)}
This will match any string like {some content} which does not contain any other group like {some content}. You can test this here.
You can capture this using:
var str = "samir patel {samirpatel#test1.com{sam#somedomain.com}}";
var regex = new Regex("{([^{}]*)}");
var matches = regex.Matches(str);
var output = matches[0].Groups[1].Value;
// output == "sam#somedomain.com"
Or more simply:
var str = "samir patel {samirpatel#test1.com{sam#somedomain.com}}";
var output = Regex.Match(str, "{([^{}]*)}").Groups[1].Value;
// output == "sam#somedomain.com"
You could get this result using (?<=\{)[^{}]*(?=\}), assuming a language other than JavaScript. In C#, for example, that's
result = Regex.Match(str, #"(?<=\{)[^{}]*(?=\})").Value;
If you're using JavaScript, use \{([^{}]*)\} and access $1 for the match result:
var myregexp = /\{([^{}]*)\}/;
var match = myregexp.exec(subject);
if (match != null) {
result = match[1];
}

2 matchcollections to 1 list

Hi guys I have 2 MatchCollection:
MatchCollection users_ids = Regex.Matches(result, #"url"":""(.*?)""");
MatchCollection users_names = Regex.Matches(result, #"fullname"":""(.*?)""");
The number of mathces of 2 collections is equаl
I need to join all Matches to 1 List. Smth like this:
foreach (Match match in users_ids)
{
string id = match.Groups[1].Value.ToString();
// string name = users_names(every match) .Groups[1].Value.ToString();
online_list.Add(id + "|" + name);
}
Any solutions?=\
This looks like the perfect application of Zip, which goes through two enumerations, taking the item at the current index of each and mapping them into a result using the given function:
var matches = users_ids.Cast<Match>()
.Zip(users_names.Cast<Match>(),
(id, name) => id.Groups[1].Value + "|" + name.Groups[1].Value);

Find the matching word C#

I have 3 strings, i would like find matches
http://www.vkeong.com/2011/food-drink/heng-bak-kut-teh-delights-taman-kepong/#comments
http://www.vkeong.com/2009/food-drink/sen-kee-duck-satay-taman-desa-jaya-kepong/
http://www.vkeong.com/2008/food-drink/nasi-lemak-wai-sik-kai-kepong-baru/
for each link above=="nasi-lemak"
{
found!
}
If you're just looking to see if a longer string contains a specific shorter string, use String.Contains.
For your example:
string[] urlStrings = new string[]
{
#"http://www.vkeong.com/2011/food-drink/heng-bak-kut-teh-delights-taman-kepong/#comments"
#"http://www.vkeong.com/2009/food-drink/sen-kee-duck-satay-taman-desa-jaya-kepong"
#"http://www.vkeong.com/2008/food-drink/nasi-lemak-wai-sik-kai-kepong-baru/"
}
foreach(String url in urlStrings)
{
if(url.Contains("nasi-lemak"))
{
//Your code to handle a match here.
}
}
You want the String.IndexOf method.
foreach(string url in url_list)
{
if(url.IndexOf("nasi-lemak") != -1)
{
// Found!
}
}
Surely we also need a LINQ answer :)
var matches = urlStrings.Where(s => s.Contains("nasi-lemak"));
// or if you prefer query form. This is really the same as above
var matches2 = from url in urlStrings
where url.Contains("nasi-lemak")
select url;
// Now you can use matches or matches2 in a foreach loop
foreach (var matchingUrl in matches)
DoStuff(matchingUrl);

Categories