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
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 2 years ago.
Improve this question
I have a string that has key-value pair as comma-separated and I want to split the string into key-value. I have a problem in splitting if the value has a comma.
Below is the sample string
job=123,Auto=Workflow1,Workflow2,debug=true
and I want to split the above string into key-value as below
job=123
Auto=Workflow1,Workflow2
debug=true
How can I do this?
We can try splitting on the following regex pattern:
,(?=[^,]+=)
This will match any comma for which we can find an = following without crossing over another comma. This rules out the in between commas which we don't want to split.
Sample code:
string input = "job=123,Auto=Workflow1,Workflow2,debug=true";
string[] parts = Regex.Split(input, #",(?=[^,]+=)");
foreach (string part in parts)
Console.WriteLine(part);
This prints:
job=123
Auto=Workflow1,Workflow2
debug=true
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.
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 6 years ago.
Improve this question
Good Afternoon,
I would like a regex to validate if the user typed two names or more and not only the first name, as in this example:
Ex: Fabrício (no match)
Ex:
Fabrício Oliveira (match)
Fabrício Oliveira Xavier (match)
Note: The expression must contain accents
Here's a Regex that only relies on a whitespace separator:
^\S+(\s\S+)+$
It makes these assumptions:
No name has a space, tab, or newline in it
Names are separated by exactly one space, tab, or newline
* this is based on #juharr's comment but with the parentheses to allow more than two names.
Edit: You can play around with this Regex here https://regex101.com/r/nS3hN8/1
Edit2: Added the beginning and ending anchors to the regex
Try this regular expression which matches two any length words separated by a space:
new Regex(#"\w+ \w+");
Really you could just use this:
if (Regex.Match(stringname, #"\w+\s\w+").Success)
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)