C# Regex for Web.config Rewrite Url - c#

I have the following url
www.localproject.com:843/user/validate/eyJhbGciOiJodHRwOi8vd3d3LnczLm9yZy8yMDAxLzA0L3htbGRzaWctbW9yZSNobWFjLXNoYTI1NiIsInR5cCI6IkpXVCJ9eyJ0ZW1wVXJsIjoie1wiQ3VzdG9tZXJJZFwiOjEsXCJDb3Vyc2VJZFwiOjEsXCJUb2tlblwiOm51bGwsXCJFeHBpcnlcIjpcIjIwMTgtMDQtMThUMTc6MzU6MTMuOTQ2MjM2NCswNTowMFwifSJ9uvm7jZ3us5UFa1hqh4bod2cSamcxF2rRUbfxs7DHQs
from which I need to extract only 10 to 30 characters after validate including numbers etc. For example, I need only this
eyJhbGciOiJodHRwOi8vd3d
what should be the regex? I tried following but it not working
^api/user/validate/(^([a-zA-Z\d]){50})

Try
api\/user\/validate\/(.{23})
Can see it working here - https://regex101.com/r/Xr6Ueo/1

You can do this with regex or Substring. If the string is stored in web.config you can still get to it with ConfigurationManager. I recommend taking the parts you do not need out of the Uri. This way once you deploy to prod there's less of a chance of things breaking down.
var input = new Uri("www.localproject.com:843/user/validate/eyJhbGciOiJodHRwOi8vd3d3LnczLm9yZy8yMDAxLzA0L3htbGRzaWctbW9yZSNobWFjLXNoYTI1NiIsInR5cCI6IkpXVCJ9eyJ0ZW1wVXJsIjoie1wiQ3VzdG9tZXJJZFwiOjEsXCJDb3Vyc2VJZFwiOjEsXCJUb2tlblwiOm51bGwsXCJFeHBpcnlcIjpcIjIwMTgtMDQtMThUMTc6MzU6MTMuOTQ2MjM2NCswNTowMFwifSJ9uvm7jZ3us5UFa1hqh4bod2cSamcxF2rRUbfxs7DHQs");
var environmentAgnosticPath = input.Segments[input.Segments.Length - 1];//take just the part after /validate
//example output: eyJhbGciOiJodHRwOi8vd3d
//I don't know what you mean by 10 - 30 since the example is 23 characters
var resultFromSubstring = environmentAgnosticPath.Substring(0,23);
var pattern = #"[A-Za-z0-9]{10,23}";
Regex regex = new Regex(pattern);
var resultFromRegex = regex.Match(environmentAgnosticPath).Value;
And generally regex101 is a great playground for figuring out the regex part.

Related

Pattern not correct with regular expressions in c#

i am doing a project , and i want to remove from a string http protocoll. In my excel sheet there are two types one is http://www.email#domain.com and the other is http://email#domain.com.I have tried so many combinations but i can't find the right one.
My code only works with the first type and not with the second one
var website_domain_in_excel = list_of_information_in_excel[2];
string pattern = "(http://\\www.)";
Console.WriteLine(Regex.Replace(website_domain_in_excel, pattern, String.Empty));
Thank you for your time
The pattern you want is this:
string pattern = #"http:\/\/(?:www\.)?"
This matches http:// and then an optional non-capturing group matching www..
You can see an explanation of the regex here and this fiddle for a working demo in C#.
You can use the string: "http?://?www.|http?://" which matches either "http://www." or "http://".
The code would look like this:
var website_domain_in_excel = list_of_information_in_excel[2];
string pattern = #"http:\/\/www.|http:\/\/";
Console.WriteLine(Regex.Replace(website_domain_in_excel, pattern, String.Empty));
A non-regex solution:
var eml = "http://www.email#domain.com";
eml = eml.Replace("http://", "").Replace("www.", "");
// eml now is "email#domain.com"
You might want to test that that "www." only appears at the start. The (unusual) "email#www.domain.com" should remain intact.
But if you really want a regex:
eml = Regex.Replace(eml, "^https?://(www\\.)?", "");
This also catches "https", because of the ? after that "s"
It will also find and replace an optional "www.", but only at the start

RegEx to extract partial string

So simple but I'm struggling, I do RegExp every 2 years or so , so I'm rusty
I have these two url strings
http://localhost:58876/Products/Product1
https://localhost:58876/Products/Product1
The result I want is
localhost:58876
Basically remove the http(s):// and everything after the first single / so I end up with the domain with or without the port number
P.S: I'm working with C#
This worked for me (tested int notepad++):
(\w+:\d+)
You can use the following regex to split the URL:
((http[s]?|ftp):/)?/?([^:/\s]+)(:([^/]))?((/\w+)/)([\w-.]+[^#?\s]+)(\?([^#]))?(#(.))?
The RegEx positions 3 and 5 are those you are looking for.
(^[^h]|\/\/)([\w\d\:\#\.]+:?[\d]?+)
then in c#:
string address = ...
char[] MyChar = {'/'};
string NewString = address.TrimStart(MyChar);
EDIT: also worked with localhost:58876/Products/Product1
!
Just match anything but a slash: /^https?:\/\/([^\/]+)\/.*$/
var url = 'http://localhost:58876/Products/Product1';
var match = url.match(/^https?:\/\/([^\/]+)\/.*$/);
if(match&&match.length>0)document.write(match[1]);
Even shorter: /\/\/([^\/]+)/. Note that there are (a lot) better ways to parse URLs. Depending on your platform, there’s PHP’s parse_url, NodeJS’s url module or libraries like uri.js that handle the many faces of valid URIs.

Writing a regex command to split an array in c#

Regex is one of those things I've wanted to be able to write myself and although I have a basic understand of how it works I've never found myself in the situation where I needed to use it where it doesn't exist already widely on the web (such as for validating email addresses).
A problem that I have is that I am receiving a string which is comma separated, however some of the string values contain commas also. For example I might receive:
$COMMAND=1,2,3,"string","another,string",4,5,6
Generally I will never receive anything like this, however the device sending me this string array allows for it to happen so I would like to be able to split the array accordingly if it ever were to occur.
So obviously just splitting it like so (where rawResponse has the $COMMAND= part removed:
string[] response = rawResponse.Split(',');
Is not good enough! I think regex is the correct tool for the job, could anyone help me write it?
string rawResponse = #"1,2,3,""string"",""another,string"",4,5";
string pattern = #"[^,""]+|""([^""]*)""";
foreach(Match match in Regex.Matches(rawResponse, pattern))
// use match.Value
Results:
1
2
3
"string"
"another,string"
4
5
If you need response as array of strings you can use Linq:
var response = Regex.Matches(rawResponse, pattern).Cast<Match>()
.Select(m => m.Value).ToArray();
string originalString = #"1,2,3,""string"",""another,string"",4,5,6";
string regexPattern = #"(("".*?"")|(.*?))(,|$)";
foreach(Match match in Regex.Matches(originalString, regexPattern))
{
}

Returning the regular expression match as part of a split (or equivalent functionality)

I am trying to parse through some log files and put them into a database for analysis. A single line looks something like this:
2012-09-30 17:16:27,213 [39] (boxes) ERROR Assembly.Places [(null)] - Error while displaying a thing
I have made a regular expression that works well for pulling out the date in front and breaking up the lines that way, but I lose the date itself. This is a pretty important bit of data, and I don't want to lose it!
I cannot just do this by \r\n, because some logs are fatal errors that include stack traces for the developers. Those, obviously, use \r\n to make them readable.
My current code looks like this for reference:
var logpath = Directory.GetFiles(#"C:\a\directory", "*.log");
foreach (var log in logpath)
{
var fileStream = new StreamReader(log);
var fileString = fileStream.ReadToEnd();
var records = Regex.Split(fileString, "[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2},[0-9]{3}");
...
}
Split() will always remove the matched delimiter. The trick is not to match any actual text, but rather a position in the string.
This is done through zero-width look-ahead:
var datePattern = "^(?=[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2},[0-9]{3})";
var datePositions = new Regex(datePattern, RegexOptions.Multiline);
// ...
Regex.Split(fileString, datePositions);
You should match instead of splitting
This is the regex.Use singleLine Mode
([0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2},[0-9]{3})(.*?)((?=[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2},[0-9]{3}|$))
Group 1 contains date
Group 2 contains the required date
NOTE
The regex is conceptually like this.
(yourDate)(.*?yourdata)(?=till the other date|$)
Dont forget to use singlelineMode
Well, I'm not an expert on the subject but I did found this: Regex.Match.
From what I see you can receive the first match of the date format with a Match object
which has all kind of nice properties that put together you can probably cut the parts you want.
p.s. also exists a Regex.Matches which will return all matches in the file, might be easier for use.
Sorry I don't have time for to find a complete code example.
good day

Extract substring from string with Regex

Imagine that users are inserting strings in several computers.
On one computer, the pattern in the configuration will extract some characters of that string, lets say position 4 to 5.
On another computer, the extract pattern will return other characters, for instance, last 3 positions of the string.
These configurations (the Regex patterns) are different for each computer, and should be available for change by the administrator, without having to change the source code.
Some examples:
Original_String Return_Value
User1 - abcd78defg123 78
User2 - abcd78defg123 78g1
User3 - mm127788abcd 12
User4 - 123456pp12asd ppsd
Can it be done with Regex?
Thanks.
Why do you want to use regex for this? What is wrong with:
string foo = s.Substring(4,2);
string bar = s.Substring(s.Length-3,3);
(you can wrap those up to do a bit of bounds-checking on the length easily enough)
If you really want, you could wrap it up in a Func<string,string> to put somewhere - not sure I'd bother, though:
Func<string, string> get4and5 = s => s.Substring(4, 2);
Func<string,string> getLast3 = s => s.Substring(s.Length - 3, 3);
string value = "abcd78defg123";
string foo = getLast3(value);
string bar = get4and5(value);
If you really want to use regex:
^...(..)
And:
.*(...)$
To have a regex capture values for further use you typically use (), depending on the regex compiler it might be () or for microsoft MSVC I think it's []
Example
User4 - 123456pp12asd ppsd
is most interesting in that you have here 2 seperate capture areas. Is there some default rule on how to join them together, or would you then want to be able to specify how to make the result?
Perhaps something like
r/......(..)...(..)/\1\2/ for ppsd
r/......(..)...(..)/\2-\1/ for sd-pp
do you want to run a regex to get the captures and handle them yourself, or do you want to run more advanced manipulation commands?
I'm not sure what you are hoping to get by using RegEx. RegEx is used for pattern matching. If you want to extract based on position, just use substring.
It seems to me that Regex really isn't the solution here. To return a section of a string beginning at position pos (starting at 0) and of length length, you simply call the Substring function as such:
string section = str.Substring(pos, length)
Grouping. You could match on /^.{3}(.{2})/ and then look at group $1 for example.
The question is why? Normal string handling i.e. actual substring methods are going to be faster and clearer in intent.

Categories