I have to check if these two url match a pattern (or 2, to be more accurate). If so, I'd like to extract some portion of data.
1) /selector/en/any-string-chain-you-want.aspx?FamiId=32
Then I need to extract "en" and "32" into variables. To me the regex expression should like roughly something like /selector/{0}/any-string-chain-you-want.aspx?FamiId={1}
2) /selector/en/F/32/any-string-chain-you-want.html
where en and 32 must be assigned into variables. So:
/selector/{0}/F/{1}/any-string-chain-you-want.html
{0}: 2 letters language code such as en, fr, nl, es,...
{1}: family id integers (2 or 3 numbers) such as 12, 38, 124, etc but not 1212
Any idea on how to achieve it?
Thanks in advance,
Roland
This is the regex:
/selector/([a-z]{2})/.+?\.aspx\?FamiId=([0-9]+)
Code:
var regex = new Regex(#"/selector/([a-z]{2})/.+?\.aspx\?FamiId=([0-9]+)");
var test = "/selector/en/any-string-chain-you-want.aspx?FamiId=32";
foreach (Match match in regex.Matches(test))
{
var lang = match.Groups[1].Value;
var id = Convert.ToInt32(match.Groups[2].Value);
Console.WriteLine("lang: {0}, id: {1}", lang, id);
}
Regex for second case: /selector/([a-z]{2})/F/([0-9]+)/.+?\.html (code doesn't change)
You should have a look at this tutorial on Regular Expressions.
You could use the following expressions:
\/selector\/([a-z]{2})\/.*\.aspx\?FamiId=([0-9]{2,3})
and
\/selector\/([a-z]{2})\/F\/([0-9]{2,3})\/.*\.html
You can try with:
^/.*?/(\w{2})/(?:F/|.*?FamiId=)(\d{2,3}).*$
It works for both urls.
Case 1
private const string Regex1 = #"/selector/(\w\w)/.+\.aspx?FamiId=(\d+)";
Case 2
private const string Regex2 = #"/selector/(\w\w)/F/(\d+)/.+\.html";
Usage
Match m = Regex.Match(myString, Regex2);
string lang = m.Groups[1].Value;
string numericValue = m.Groups[2].Value;
string str = #"/selector/en/any-string-chain-you-want.aspx?FamiId=32";
Match m = Regex.Match(str, #"/selector/(\w{2})/.+\.aspx\?FamiId=(\d{2,3})");
string result = String.Format(#"/selector/{0}/F/{1}/any-string-chain-you-want.html", m.Groups[1].Value, m.Groups[2].Value);
There you go.
Its useful to learn a bit of regular expression for cases like this. RegExr is a free online RegEx building tool. However, the most useful I have found is Expresso
you can use something like that
String urlSchema1= #"/selector/(<lang>\w\w)/.+\.aspx?FamiId=(<FamiId>\d+)";
Match mExprStatic = Regex.Match(inputString,urlSchema1, RegexOptions.IgnoreCase | RegexOptions.Singleline);
if (mExprStatic.Success || !string.IsNullOrEmpty(mExprStatic.Value))
{
String language = mExprStatic.Groups["lang"].Value;
String FamId = mExprStatic.Groups["FamId"].Value;
}
String urlSchema2= #"/selector/(<lang>\w\w)/F/(<FamId>\d+)/.+\.html";
Match mExprStatic = Regex.Match(inputString,urlSchema2, RegexOptions.IgnoreCase | RegexOptions.Singleline);
if (mExprStatic.Success || !string.IsNullOrEmpty(mExprStatic.Value))
{
String language = mExprStatic.Groups["lang"].Value;
String FamId = mExprStatic.Groups["FamId"].Value;
}
Related
Im new in C#, I have the following string, want to extract resolution from it string could be any length.
e.g.
1100x1200#60
or
800x600#25
and I want to extract 1100 and 1200 in two different variables using regular expression.
Thanks
Use the below regex and grab the resolution values from group index 1 and 2.
#"(\d+)x(\d+)"
You may add lookahead to check the match the resolution only if it's followed by an # symbol.
#"(\d+)x(\d+)(?=#)"
DEMO
String input = #"1100x1200#60";
Regex rgx = new Regex(#"(\d+)x(\d+)(?=#)");
foreach (Match m in rgx.Matches(input))
{
String var1 = m.Groups[1].Value;
String var2 = m.Groups[2].Value;
Console.WriteLine(var1);
Console.WriteLine(var2);
}
IDEONE
([^x]+)x([^#]+)
Try this.Grab the capture.See demo.
http://regex101.com/r/lZ5mN8/49
I have a html string which i'm parsing which looks like below. I need to get the value of #Footer.
strHTML = "<html><html>\r\n\r\n<head>\r\n<meta http-equiv=Content-Type
content=\"text/html; charset=windows-1252\">\r\n
<meta name=Generator content=\"Microsoft Word 14></head></head><body>
<p>#Footer=CONFIDENTIAL<p></body></html>"
I have tried the below code, how do i get the value?
Regex m = new Regex("#Footer", RegexOptions.Compiled);
foreach (Match VariableMatch in m.Matches(strHTML.ToString()))
{
Console.WriteLine(VariableMatch);
}
You need to capture the value after the =. This will work, as long as the value cannot contain any < characters:
Regex m = new Regex("#Footer=([^<]+)", RegexOptions.Compiled);
foreach (Match VariableMatch in m.Matches(strHTML.ToString()))
{
Console.WriteLine(VariableMatch.Groups[1].Value);
}
You can do this with regex, but it's not necessary. One simple way to do this would be:
var match = strHTML.Split(new string[] { "#Footer=" }, StringSplitOptions.None).Last();
match = match.Substring(0, match.IndexOf("<"));
This assumes that your html string only has one #Footer.
Your regex will match the string "#Footer". The value of the match will be "#Footer".
Your regex should look like this instead :
Regex regex = new Regex("#Footer=[\w]+");
string value = match.Value.Split('=')[1];
Use a matching group.
Regex.Matches(strHTML, #"#Footer=(?<VAL>([^<\n\r]+))").Groups["VAL"].Value;
If that's all your string, we can use string methods to solve it without touching regex stuff:
var result = strHTML.Split(new string[]{"#Footer=", "<p>"}, StringSplitOptions.RemoveEmptyEntries)[1]
I want to filter the following string with the regular expressions:
TEST^AB^^HOUSE-1234~STR2255
I wanna get only the string "HOUSE-1234" and I've to test the string always with the beginning "TEST^AB^^" and ending with the "~".
Can you please help me how the regex should look like?
You can use \^\^(.*?)\~ pattern which matches start with ^^ and ends with ~
string s = #"TEST^AB^^HOUSE-1234~STR2255";
Match match = Regex.Match(s, #"\^\^(.*?)\~", RegexOptions.IgnoreCase);
if (match.Success)
{
string key = match.Groups[1].Value;
Console.WriteLine(key);
}
Output will be;
HOUSE-1234
Here is a DEMO.
string input = "TEST^AB^^HOUSE-1234~STR2255";
var matches = Regex.Matches(input, #"TEST\^AB\^\^(.+?)~").Cast<Match>()
.Select(m => m.Groups[1].Value)
.ToList();
string pattern=#"\^\^(.*)\~";
Regex re=new Regex(pattern);
With the little information you've given us (and assuming that the TEST^AB isn't necessarily constant), this might work:
(?:\^\^).*(?:~)
See here
Or if TEST^AB is constant, you can throw it in too
(?:TEST\^AB\^\^).*(?:~)
The important part is to remember that you need to escape the ^
Don't even need the RegEx for something that well defined. If you want to simplify:
string[] splitString;
if (yourstring.StartsWith("TEST^AB^^"))
{
yourstring = yourstring.Remove(0, 9);
splitString = yourstring.Split('~');
return splitString[0];
}
return null;
(TEST\^AB\^\^)((\w)+-(\w+))(\~.+)
There are three groups :
(TEST\^AB\^\^) : match yours TEST^AB^^
((\w)+\-(\w+)) : match yours HOUSE-123
(\~.+) : match the rest
You should do this without regex:
var str = "TEST^AB^^HOUSE-1234~STR2255";
var result = (str.StartsWith("TEST^AB^^") && str.IndexOf('~') > -1)
? new string(str.Skip(9).TakeWhile(c=>c!='~').ToArray())
: null;
Console.WriteLine(result);
I have a string and need the letters from said string.
string s = "EMA123_33"; // I need "EMA"
string s = "EMADRR123_33"; // I need "EMADRR"
I am using C# in Visual Studio 2008.
You can try this:
var myString = "EMA123_33";
var onlyLetters = new String(myString.Where(Char.IsLetter).ToArray());
please note: this version will find "e" just like "E" - if you need only upper-case letters then do something like this:
var myString = "EMA123_33";
var onlyLetters = new String(myString.Where(c => Char.IsLetter(c) && Char.IsUpper(c)).ToArray());
You can use a regular expression to replace all non-letters:
string s2 = Regex.Replace(s, #"[^A-Z]+", String.Empty);
If you're just after the initial letters, i.e. those at the start of the string (your examples are a bit unclear in that I don't know what would happen to letters at the end of the string), you can use a different Regex:
string s2 = Regex.Replace(s, #"(\p{L}+).*", "$1");
Regex MyRegex = new Regex("[^a-z]", RegexOptions.IgnoreCase);
string s = MyRegex.Replace(#"your 76% strings &*81 gose _ here and collect you want_{ (7 438 ?. !`", #"");
Console.WriteLine(s);
output
yourstringsgosehereandcollecyouwant
please help me this problem.
I want to split "-action=1" to "action" and "1".
string pattern = #"^-(\S+)=(\S+)$";
Regex regex = new Regex(pattern);
string myText = "-action=1";
string[] result = regex.Split(myText);
I don't know why result have length=4.
result[0] = ""
result[1] = "action"
result[2] = "1"
result[3] = ""
Please help me.
P/S: I am using .NET 2.0.
Thanks.
Hello, I tested with string: #"-destination=C:\Program Files\Release" but it have inaccurate result, I don't understand why result's length = 1. I think because it has a white space in string.
I want to split it to "destination" & "C:\Program Files\Release"
More info: This is my requirement:
-string1=string2 -> split it to: string1 & string2.
In string1 & string2 don't contain characters: '-', '=', but they can contain white space.
Please help me. Thanks.
Don't use split, just use Match, and then get the results from the Groups collection by index (index 1 and 2).
Match match = regex.Match(myText);
if (!match.Success) {
// the regex didn't match - you can do error handling here
}
string action = match.Groups[1].Value;
string number = match.Groups[2].Value;
Try this (updated to add Regex.Split):
string victim = "-action=1";
string[] stringSplit = victim.Split("-=".ToCharArray());
string[] regexSplit = Regex.Split(victim, "[-=]");
EDIT: Using your example:
string input = #"-destination=C:\Program Files\Release -action=value";
foreach(Match match in Regex.Matches(input, #"-(?<name>\w+)=(?<value>[^=-]*)"))
{
Console.WriteLine("{0}", match.Value);
Console.WriteLine("\tname = {0}", match.Groups["name" ].Value);
Console.WriteLine("\tvalue = {0}", match.Groups["value"].Value);
}
Console.ReadLine();
Of course, this code have issues if your path contains - character
In .NET Regex, you can name your groups.
string pattern = #"^-(?<MyKey>\S+)=(?<MyValue>\S+)$";
Regex regex = new Regex(pattern);
string myText = "-action=1";
Then do a "Match" and get the values by your group names.
Match theMatch = regex.Match(myText);
if (theMatch.Success)
{
Console.Write(theMatch.Groups["MyKey"].Value); // This is "action"
Console.Write(theMatch.Groups["MyValue"].Value); // This is "1"
}
What's wrong with using string.split()?
string test = "-action=1";
string[] splitUp = test.Split("-=".ToCharArray());
I admit, though that this still gives you possibly more parameters than you'd like to see in the split up array...
[0] = ""
[1] = "action"
[2] = "1"
In his talk Regular Expression Mastery, Mark Dominus attributes the following helpful rule to Learning Perl author (and fellow StackOverflow user) Randal Schwartz:
Use capturing or m//g [or regex.Match(...)] when you know what you want to keep.
Use split when you know what you want to throw away.