Regex to shrink Path.Combine Relative to Absolute [closed] - c#

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have 2 file paths:
absolute:
/Content/assets2/otherfolder/another/this/
relative:
../../../../assets/img/logo.gif
when I do Path.Combine(absolute, relative) I get:
/Content/assets2/otherfolder/another/this/../../../../assets/img/logo.gif
which works fine, but what I want to get:
/Content/assets/img/logo.gif
I need a Regex or code that removes the "../" with the corresponding folder:
/Content/assets2/otherfolder/another/this/../../../../assets/img/logo.gif
/Content/assets2/otherfolder/another/../../../assets/img/logo.gif
/Content/assets2/otherfolder/../../assets/img/logo.gif
/Content/assets2/../assets/img/logo.gif
finally into:
/Content/assets/img/logo.gif

You don't need a Regex for that. Actually, I think it would be impossible to define in a reliable way in Regex. Instead, use Path.GetFullPath:
string combined = Path.Combine(path1, path2);
string prettyPath = Path.GetFullPath(combined);

Related

Find a specific number in the middle of a string and remove it from list of numbers [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I don't have much code to show but I am trying to remove a substring (individual number) from a longer string is a series of numbers.
Ex: Main String is "11,12,15,16,55,33,88,100,121,155,115"
Need to find number 16 and remove it from the string leaving...
11,12,15,55,33,88,100,121,155,115
They are a list of id's from a database so I can't just change them to strings. Also how do I remove it as if it wasn't there?
string numbers = "11,12,15,16,55,33,88,100,121,155,115";
numbers = string.Join(",", numbers.Split(',').Where(num => num != "16"));
But why don't you use a List<int> instead for database ID's?
In this specific use case I would split the string using "," as the separator, remove the element that matches, then join the elements again.

Splitting string containing spaces in C# Regex [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
For example I have this string:
BTW This is a comment "hahaha"
BTW is a comment operator and all statements after it are ignored.
I need to put BTW as 'comment_operator' and 'This is a comment "hahaha"' as 'comment' in a datagridview.
But I can't do it because I used space as a delimiter in my code, so 'This is a comment "hahaha"' will be concatenated too but I need it as it is.
Can someone enlighten me with this? Thanks.
I assume you want the separate 2 parts by the first occurrence of space. You can use the code below:
string text = #"BTW This is a comment ""hahaha""";
string comment_operator = text.Substring(0, text.IndexOf(' '));
string comment = text.Substring(comment_operator.Length + 1);

c# Regular Expression for the text below? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have strings of array.
One of the strings:
myText = "[Date].[Hierarchy].[Year].&[2005].&[Q1].&[Jan]"
The [Date].[Hierarchy].[Year].& part is always the same, but the rest is changing depending on the today date.
So it would be for today like [Date].[Hierarchy].[Year].&[2013].&[Q3].&[Jul], but in the array I also have other strings that like [Date].[Hierarchy].[Year].&[2005].&[Q1].&[Jan].&[20].
The regex should opt those out also.
I am only interested in strings from array like [Date].[Hierarchy].[Year].&[2005].&[Q1].&[Jan]", [Date].[Hierarchy].[Year].&[2005].&[Q1].&[Feb], [Date].[Hierarchy].[Year].&[2005].&[Q1].&[Mar], ....
I hope you get the pattern.
This Regex should do it. The year is currently set to 4 digits, if you only want it to match 2005, replace the \d{4} bit.
^\[Date\]\.\[Hierarchy\]\.\[Year\]\.&\[\d{4}\]\.&\[Q1\]\.&\[[A-Z-a-z]{3}\]$
Here's the result:
[Date].[Hierarchy].[Year].&[2005].&[Q1].&[Jan] // matches
[Date].[Hierarchy].[Year].&[2013].&[Q3].&[Jul] // no match
[Date].[Hierarchy].[Year].&[2005].&[Q1].&[Jan].&[20] // no match
Edit to your comment: Make sure you put an # before the string declaration.
var pattern = new Regex(#"^\[Date\]\.\[Hierarchy\]\.\[Year\]\.&\[\d{4}\]\.&\[Q1\]\.&\[[A-Z-a-z]{3}\]$");
var matches = pattern.IsMatch("[Date].[Hierarchy].[Year].&[2005].&[Q1].&[Jan]");

How can I extract a string using C# Substring [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I want to extract the browser name and Operating system in differant strings from
this string. How can I use substring to extract them from
{ Browser/leavethis (Operating System) Leavethis/Leavethis (Leavethis, Leavethis) Leavethis/Leavethis/Leavethis }
Instead of splitting the useragent, you should use the properties already supplied the Asp.net Framework to detect the browser/os.
Use this
to get the browser name Request.Browser.Browser and browser version Request.Browser.MajorVersion
to get the Os name Request.Browser.Platform and os version Request.UserAgent
A regex such as the following should work, but I suspect #bastos.sergio's answer is the correct way to get what you need.
Regex r = new Regex(#"{\s+(?<Browser>\w+)/[^(]+\((?<OperatingSystem>[^)]+).+");
string browser;
string OS;
var match = r.Match(s);
if (match.Success)
{
browser = match.Groups["Browser"].Value;
OS = match.Groups["OperatingSystem"].Value;
}

Does string.Replace("a", "b") automatically check if "a" exists? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
string banana = "banana apple";
banana.Replace("apple", "pie");
If I want to replace apple with pie, can I do it like that, or do I need to use the following?
if(banana.Contains("apple"))
banana.Replace("apple", "pie");
You just have to read msdn: ( or try it out yourself )
Return Value Type: System.String A string that is equivalent to the
current string except that all instances of oldValue are replaced with
newValue. If oldValue is not found in the current instance, the method
returns the current instance unchanged.
Side-note: since strings are immutable(you cannot change the instance) you have to reassign a new string if you want to change the old:
banana = banana.Replace("apple", "pie");

Categories

Resources