i have a string suppose that one
http://www.whitelabelhosting.co.uk/flight-search.php?dept=any&journey=R&DepTime=0900
and now what i am doing is here in c sharp
string linkmain = link.Replace("&DepTime=", "&DepTime=" + journey);
but the time is being added as 09000900
and in case of
string linkmain = link.Replace("Journey=", "Journey="+journey);
journey added as RR
so i have to get the value of R that is after Journer=? AND deptTime=?
that are not same every time so how to get them during replace as they are present just after where ? sign is marked
this is a post operation so parameter are different like
journey :
R
M
O
and time :
0900 , 1200 , 0400
Use HttpUtility.ParseQueryString(url) with will return a NameValueCollection. You can then loop this collection to manipulate the data how you want and build the new url from that.
https://msdn.microsoft.com/en-us/library/ms150046(v=vs.110).aspx
You may want to try out the following regex. Regex101 link
((?:\?.*?&|\?)journey=)[^&]*
Try out the following code to replace the value of journey to replacement
string url = "http://www.whitelabelhosting.co.uk/flight-search.php?dept=any&journey=R&DepTime=0900";
string newUrl = Regex.Replace(url, #"((?:\?.*?&|\?)journey=)[^&]*", "$1"+"replacement");
Remember to add the following to your file:
using System.Text.RegularExpressions;
You can do the same for DepTime using the following regex:
((?:\?.*?&|\?)DepTime=)[^&]*
Related
I am developing website using asp.net. In there I mainly use URL to pass parameters.
I have URL structure like this
http://localhost:51247/yyy/zzz/hrforum/(if its in my local PC)
http://test.com/yyy/zzz/hrforum/
I need to detect that zzz part and replace it with another word. I tried many things including Regex patterns but seems I am doing git wrong way. Please help me to detect it. Modify it and rebuild the URL
Codes I tried
Regex myRegex = new Regex(#"/([\w\s]+?\;){2}/");
var match = myRegex.Match(fullUrl);
var firstName = match.Groups[0].Value;
But this is not working.
The easiest method of doing this would be to use the Uri.Segments property. For example:
Uri uriAddress1 = new Uri("http://test.com/yyy/zzz/hrforum/");
Uri uriAddress2 = new Uri("ttp://localhost:51247/yyy/zzz/hrforum/");
Console.WriteLine(uriAddress1.Segments[2] == uriAddress2.Segments[2]);
Console.WriteLine("Segment 2 of Address 1: {0} Segment 2 of Address 2: {1}", uriAddress1.Segments[2].Trim('/'),uriAddress2.Segments[2].Trim('/'));
Output:
True
Segment 2 of Address 1: zzz Segment 2 of Address 2: zzz
I'm not sure what you want to achieve but to answer this question:
How to detect specific part of the URL and modify it?
I think you can use Uri class instead of using Regex.
var uri = new Uri("http://test.com/yyy/zzz/hrforum/");
var pathName = uri.PathAndQuery;
foreach (var item in pathName.Split('/'))
{
Console.WriteLine(item);
}
// output:
// yyy
// zzz
// hrforum
I am pulling file names into a variable (#[User::FileName]) and attempting to extract the work order number (always 5 numbers with underscores on both sides) from that string. For example, a file name would look like - "ABC_2017_DEF_9_12_GHI_35132_S5160.csv". I want result to return "35132". I have found examples of how to do it such as this SUBSTRING(FileName,1,FINDSTRING(FileName,"_",1) - 1) but the underscore will not always be in the same location.
Is it possible to do this in the expression builder?
Answer:
public void Main()
{
string strFilename = Dts.Variables["User::FileName"].Value.ToString();
var RegexObj = new Regex(#"_([\d]{5})_");
var match = RegexObj.Match(strFilename);
if (match.Success)
{
Dts.Variables["User::WorkOrder"].Value = match.Groups[1].Value;
}
Dts.TaskResult = (int)ScriptResults.Success;
}
First of all, the example you have provided ABC_2017_DEF_9_12_GHI_35132_S5160.csv contains 4 numbers located between underscores:
2017 , 9 , 12 , 35132
I don't know if the filename may contains many a 5 digits number can occurs many times, so in my answer i will assume that the number you want to return is the last occurrence of the number made of 5 digits.
Solution
You have to use the Following Regular Expression:
(?:_)\K[0-9][0-9][0-9][0-9][0-9](?=_)
DEMO
Or as #MartinSmith Suggested (in a comment), you can use the following RegEx:
_([\d]{5})_
Implemeting RegEx in SSIS
First add another Variable (Ex: #[User::FileNumber])
Add a Script Task and choose #[User::Filename] variable as ReadOnlyVariable, and #[User:FileNumber] as ReadWriteVariable
Inside the script task use the following code:
using System.Text.RegularExpressions;
public void Main()
{
string strFilename = Dts.Variables["filename"].Value.ToString();
string strNumber;
var objRegEx = new Regex(#"(?:_)\K[0-9][0-9][0-9][0-9][0-9](?=_)");
var mc = objRegEx.Matches(strFilename);
//The last match contains the value needed
strNumber = mc[mc.Count - 1].Value;
Dts.Variables["FileNumber"].Value.ToString();
Dts.TaskResult = (int)ScriptResults.Success;
}
do the other pieces mean something?
anyway you can use a script task and split function.
pass in #fileName as readonly, and #WO as readwrite
string fn = Dts.Variables["fileName"].Value;
string[] parts = fn.Split('_');
//Assuming it's always the 7th part
// You could extract the other parts as well.
Dts.Variables["WO"].Value = part(6);
I would do this with a Script Transformation (or Script Task if this is not in a DataFlow) and use a Regex.
This is what I tried:
string myURL= "http://mysite.com/articles/healthrelated";
String idStr = myURL.Substring(myURL.LastIndexOf('/') + 1);
I need to fetch "healthrelated" ie the text after the last slash in the URL. Now the problem is that my URL can also be like :
"http://mysite.com/articles/healthrelated/"
ie "a Slash" at the end of that text too. Now the last slash becomes the one AFTER "healthrelated" and so the result I get using
String idStr = myURL.Substring(myURL.LastIndexOf('/') + 1);
is empty string..
what should my code be like so I always get that text "healthrelated" no matter if there's a slash in the end or not. I just need to fetch that text somehow.
Try this.
var lastSegment = url
.Split(new string[]{"/"}, StringSplitOptions.RemoveEmptyEntries)
.ToList()
.Last();
Why don't you use Uri class of .NET and use segments property:
http://msdn.microsoft.com/en-us/library/system.uri.segments.aspx
What you can do in this situation is either using REGEX (which I'm not an expert on, but I'm shure other ppl here are ;) ) or a simple:
string[] urlParts = myURL.Split('/');
and take the last string in this array.
Help me to parse this message:
text=&direction=re&orfo=rus&files_id=&message=48l16qL2&old_charset=utf-8&template_id=&HTMLMessage=1&draft_msg=&re_msg=&fwd_msg=&RealName=0&To=john+%3Cjohn11%40gmail.com%3E&CC=&BCC=&Subject=TestSubject&Body=%3Cp%3EHello+%D0%9F%D1%80%D0%B8%D0%B2%D0%B5%D1%82+%D1%82%D0%B5%D0%BA%D1%81%D1%82%3Cbr%3E%3Cbr%3E%3C%2Fp%3E&secur
I would like to get information in an KeyValuePair:
Key - Value
text -
direction - re
and so on.
And how to convert this: Hello+%D0%9F%D1%80%D0%B8%D0%B2%D0%B5%D1%82+%D1%82%D0%B5%D0%BA%D1%81%...
there are cyrillic character.
Thanks.
If you want to use a Regex, you can do it like this:
// I only added the first 3 keys, but the others are basically the same
Regex r = new Regex(#"text=(?<text>.*)&direction=(?<direction>.*)&orfo=(?<orfo>.*)");
Match m = r.Match(inputText);
if(m.Success)
{
var text = m.Groups["text"].Value; // result is ""
var direction = m.Groups["direction"].Value; // re
var orfo = m.Groups["orfo"].Value;
}
However, the method suggested by BoltClock is much better:
System.Collections.Specialized.NameValueCollection collection =
System.Web.HttpUtility.ParseQueryString(inputString);
It looks like you are dealing with a URI, better to use the proper class than try and figure out the detailed processing.
http://msdn.microsoft.com/en-us/library/system.uri.aspx
Needing a bit help getting multiple values from a string using Regex. I am fine getting single values from the string but not multiple.
I have this string:
[message:USERPIN]Message to send to the user
I need to extract both the USERPIN and the message. I know how to get the pin:
Match sendMessage = Regex.Match(message, "\\[message:[A-Z1-9]{5}\\]");
Just not sure how to get both of the values at the same time.
Thanks for any help.
Use Named Groups for easy access:
Match sendMessage = Regex.Match(message,
#"\[message:(?<userpin>[A-Z1-9]{5})\](?<message>.+)");
string pin = sendMessage.Groups["userpin"].Value;
string message = sendMessage.Groups["message"].Value;
var match = Regex.Match(message, #"\[message:([^\]]+)\](.*)");
After - inspect the match.Groups with debugger - there you have to see 2 strings that you expect.
You need to use numbered groups.
Match sendMessage = Regex.Match(message, "\\[message:([A-Z1-9]{5})(.*)\\]");
string firstMatch = sendMessage.Groups[1].Value;
string secondMatch = sendMessage.Groups[2].Value;