Filter string based on ending convention - c#

I got strings which i want to take only those of them which have ending on for instance:
-id-3 (e.g somother33-id-3)
-id-203 (e.g som78estringetc-id-203)
-id-54 (e.g fwefwefwefw-id-3)
but sometimes i am retreiving strings which looks like this one i dont want to get
som78estringetc-id-203:someotherstring3-1
i am intrested only those string which ends by -id-somedigit
So string which i would like to get are those ending by convention:
somestring-id-digit
Could anyone help me how can i achieve that?

Using regex you can simply do:
-id-[0-9]*$
If you want to exclude your other item you could try:
[a-zA-Z0-9]*-id-[0-9]*$

try to use regex.
Regex.IsMatch("asdfadid-13234234", #"\w*-Id-[0-9]*$\b", RegexOptions.IgnoreCase)

this case best would be #"\w*-Id-[0-9]*$\b" right?

Related

RegEx to Validate URL with optional Scheme

I want to validate a URL using regular expression. Following are my conditions to validate the URL:
Scheme is optional
Subdomains should be allowed
Port number should be allowed
Path should be allowed.
I was trying the following pattern:
((http|https)://)?([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?
But I am not getting the desired results. Even an invalid URL like '*.example.com' is getting matched.
What is wrong with it?
are you matching the entire string? you don't say what language you are using, but in python it looks like you may be using search instead of match.
one way to fix this is to start you regexp with ^ and end it with $.
While parsing URL's is best left to a library (since I know perl best, I would suggest something like http://search.cpan.org/dist/URI/), if you want some help debugging that statement, it might be best to try it in a debugger, something like: http://www.debuggex.com/.
I think one of the main reasons it is matching, is because you don't use beginning and ending string match markers. Meaning, no part of that string might be matching what you put in explicitly, but because you haven't marked it with beginning and end markers for the string, your regex could just be matching 'example.com' in your string, not the entire input.
Found the regular expression for my condition with help from your inputs
^(http(s)?://)?[0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*(:(0-9)*)*(\/?)([a-zA-Z0-9\-‌​\.\?\,\'\/\\\+&%\$#_]*)?$
Following code works for me in c#
private static bool IsValidUrl(string url)
{
return new Regex(#"^(http|http(s)?://)?([\w-]+\.)+[\w-]+[.\w]+(\[\?%&=]*)?").IsMatch(url) &&!new Regex(#"[^a-zA-Z0-9]+$").IsMatch(url);
}
it allows "something.anything (at least 2 later after period) with or without http(s) and www.

How do you modify the matched string using Regex.Replace?

Say I want to convert these strings:
www.myexample.com and http://www.myexample.com
into:
<a href='http://www.myexample.com'>http://www.myexample.com</a>
using Regex.Replace
I've come up with this:
Regex.Replace(string, pattern, "$&")
My problem is that I don't know how to check if the matched string $& starts with http:// and adds it if necessary.
Any ideas?
If you don't have to consider https or things like that, you could maybe use this:
Regex.Replace(string, #"(?:http://)?(.+)", "http://$1")

Remove last four characters in string that's part of url?

Here is my code:
Search Related
I want to take the text value of lblGraphicNameValue and remove the last four characters. Can I do this, and keep it inline? Or is this something I should do in the code behind?
Thanks!
I think this would work:
Search Related
or also this:
Search Related
I'm not sure, I have no experience with ASP.NET though so I don't know if it allows arbitrary code.
You can also use the Remove method:
lblGraphicNameValue.Text.Remove(lblGraphicNameValue.Text.Length - 4)

Copy an Entire String from a Keyword

So, I want to do this,
For example, there is a string called [FULLNAME]-Awesome Guy-[END],
But there are multiple strings in a list, so like:
[OTHER]-AG-[END]
[FULLNAME]-Awesome Guy-[END]
[NICKNAME]-AG-[END]
My question is, how can I find [FULLNAME] then set a string as [FULLNAME]-Awesome Guy-[END]
Can you guys help?
Thanks!
i'd probably recommend using a regular expression here if you just need something quick. if you need something more robust and able to handle breaking up the various tags, you might want to look at writing up your own basic parser to break stuff up by tag and let you search that way.
this code:
string s = "[OTHER]-AG-[END] [FULLNAME]-Awesome Guy-[END] [NICKNAME]-AG-[END]";
Regex re = new Regex(#"\[FULLNAME\][^[]+\[END\]");
Console.WriteLine(re.Match(s));
prints
[FULLNAME]-Awesome Guy-[END]
although it will give you malformed results if there is a [ character in the name somewhere.

Generate Public "ID Text" (like Stackoverflow)

I'm trying to create a simple method to turn a name (first name, last name, middle initial) into a public URL-friendly ID (like Stackoverflow does with question titles). Now people could enter all kinds of crazy characters, umlauts etc., is there something in .NET I can use to normalize it to URL-acceptable/english characters or do I need to write my own method to get this done?
Thank you!
Edit: An example (e.g. via RegEx or other way) would be super helpful!!! :)
Sounds like what you're after is a Slug Generator!
Simple method using UrlEncode
You obviously have to do something to deal with the collisions (prevent them on user creation being sensible but that means you are tied to this structure)
s => Regex.Replace(HttpUtility.UrlEncode(s), "%..", "")
This is relying on the output of UrlEncode always using two characters for the encoded form and that you are happy to have space convert to '+'
A regular expression to validate the string with the characters and lengths you wish to allow.
Think you'll have to write your own method...
Safelist of characters...
A to Z
For Each c As char In SafeList
If safe ... etc.
Next

Categories

Resources