Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
How to find between 2 words and replace them into new string?
I wanna replace same.
for example given string :
string s = "word1-word2same-word3-word4";
same is = "word2";
What i wanna :
string s = "word1-word2word2-word3-word4";
But what if given string is:
string s = "word1same-word2-word3-word4";
same is -> word1;
What i wanna in this example:
string s = "word1word1-word2-word3-word4";
How to find what word is containing same? How to do it?
I think the following regular expression replacement is what you want
var input = "word1same-word2same-word3";
var result = Regex.Replace(input, "(?<=^|-)([^-]*)same", "$1$1");
Console.WriteLine(result);
would give you
word1word1-word2word2-word3
So it will replace all the instances of "same" with whatever comes before it up to a hyphen or the beginning of the string.
Note that something like "whatsamesamesame" will give you a result of "whatsamesamewhatsamesame", basically it will only replace the last "same" after a hyphen or the beginning of the string.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 months ago.
Improve this question
I have shared the relevant image below.
Text in image ;
"C:\Users\Hp\OneDrive-blablablabla\Masaustu\EdaGorsel\4.PNG\tmanager\tmanager\t2022/12/12\r\nddd\tV1.0"
I only want 4.PNG from the above post
2.I should not use special names or filenames
3.Ex: \t between \ like this
I hope I was able to explain my problem
you could use a Regex match to get this part of the url :
private Regex _regex = new Regex(#"^.*\\(?<Image>.*\.PNG)\\.*$");
(You can test regex here)
Then use this to read the match :
var result = _regex.Match(url).Groups["Image"].Value;
Another, simplier solution, would be to use :
url.Split("\\")[6];
This works only if the searched part is always at the same spot.
You can split your strings into many shorter ones using .Split function:
string[] words = path.Split("\\");
Using the double \ to escape the character..
If you don't guarantee that the string you need is always at the same position, you can use this to fetch it from the list:
string neededString = words .FirstOrDefault(str => str.Contains(".PNG"));
This will return the first instance of any word from your string that contains ".png" in it.
there is sevral ways
if you know number of "" in your string then split it(str.Split(' \ ')) and get wanted index
use regular expersion to extract filename
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
As the title indicates, how can I pass an array of delimiters to a Regex.Split function? I'm needing to split a sentence (string) by a list of words (also strings).
You can build a regex pattern from "delimiter" words like this:
var delim = new string[] {"fox", "lazy"};
var pattern = #"(?:\s|^)(?:" + string.Join("|", delim.Select(Regex.Escape)) + #")(?:\s|$)";
\s and string anchors at the beginning and at the end ensure that delimiters include all white space around them, and that you avoid the Scunthorpe problem. Using Regex.Espace ensures that delimiters with regex meta-characters do not break your code.
The resultant pattern looks as follows:
(?:\s|^)(?:fox|lazy)(?:\s|$)
Demo 1
If you would like to keep delimiter words among the tokens, change regex to use them in a lookahead/lookbehind:
var delimGroup = "(?:"+string.Join("|", delim.Select(Regex.Escape))+")";
var pattern = #"\s(?="+delimGroup+")|(?<="+delimGroup+#")\s";
Demo 2
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Is it possible to concatenate two sub strings from a input string using Regex
Example : Input string "ABTTPQR 00100300250000" and I want take first two characters "AB" and first 9 digits "001003002" and concatenate these two string to one "AB001003002"
Much shorter variation using references:
Regex.Replace("ABTTPQR 00100300250000", #"^(\w{2})\w*\s(\d{9})\d+$", #"$1$2")
// = "AB001003002"
You can do as follows :
Regex regex = new Regex(#"(\w{2})\w*\s(\d{9})\d*");
var matches = regex.Matches("ABTTPQR 00100300250000");
String output = matches[0].Groups[1].Value + matches[0].Groups[2].Value;
//AB001003002
Regex Explanation:
(\w{2})\w*\s(\d{9})\d*
The (\w{2}) will match the first 2 chars, then it will look for more chars and a space \w*\s then it will match first 9 digits (\d{9}) and then will look for more digits \d*.
The () captures the matches into groups which are accessed in c# like shown in code sample: matches[0].Groups[1].Value for group 1 (chars) and matches[0].Groups[2].Value for group 2 (digits)
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
string [] leerArchivo;
good evening, i have to get a sub-string from a string. I have a file type "string []" that contains a string like this.
//start class ClassName xxxxx
where ClassName can be any name, then this is a description that does not interest me to capture, so if I need in the optener (ClassName)
thank you very much beforehand, I hope and make me understand.
To do this you call the "Substring" method in which you specify the first and last indexes of the characters you want to get.
string input = "OneTwoThree";
//Get first three characters
string sub = input.Substring(0, 3);
The output would be just the first 3 letters (in this case "One").
You can also specify just the first index of and the method will take everything from that character onwards
string input = "OneTwoThree";
string sub = input.Substring(3);
the output in this case will be: "TwoThree"
but it looks like you want to get the name of a class, and there is a much simpler way to do that :
typeof(MyProgram).Name
Hope it helps :)
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
So I am trying to replace the characters below from a users input which relates to a SQL column in a table.
Example:
User creates a field called My(first)try. In SQL this comes up as Myfirsttry.
I am currently write code to replace the users input with our and I am stuck.
I have this so far.
itemreplace = itemreplace.Replace("(", "");
This however, doesn't do the trick. Thoughts, comments, suggestions?
I feel like the real-world case is more complicated, and you're not indicating that, but to handle your example text My(first)try you could just chain the Replace statements:
itemreplace = itemreplace.Replace("(", "").Replace(")", "");
However, it seems like the real-world case is more along the lines of leveraging a Regex like this:
^[a-zA-Z0-9_##][a-zA-Z0-9#$#_]*$
and here is a Regex 101 that would prove that.
Then using that might look like this:
var valid = Regex.IsMatch("My(first)try", pattern);
I did reference this post, What special characters are allowed in T-SQL column name?, to determine the allowed characters for a column name.
First option:
String itemreplace = new String("My(first)try");
String charsToRemove = new String[] {"(", ")"};
foreach (char c in charsToRemove)
{
itemreplace = itemreplace.Replace(c, string.Empty);
}
Second option:
itemreplace = itemreplace.Replace("(", string.Empty).Replace(")", string.Empty);