regex to concatenate two strings in C# [closed] - c#

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)

Related

Find a word between 2 words and replace them [closed]

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.

How to pass array of delimiters to Regex.Split function? [closed]

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

How to convert string abcabc to abc in C# [closed]

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 7 years ago.
Improve this question
How to convert string like
"abcabc"
to
"abc"
Here is what want to achieve
ANDSQLP47ANDSQLP47\CTOPROD8R2
to
ANDSQLP47\CTOPROD8R2
You can use some Regex to find the repeating pattern and do the replacement:
var regex = new Regex(#"(\w+)\1\\(\w+)");
var result = regex.Replace(#"ANDSQLP47ANDSQLP47\CTOPROD8R2", #"$1\$2");
//result: ANDSQLP47\CTOPROD8R2
Regex explanation:
(\w+) : Match sequence of characters (first capture group $1)
\1 : Match same sequence of characters as first capture group
\\ : Match '\' character
(\w+) : Match sequence of characters (second capture group $2)
You can get more info about Regex on MSDN
Edit:
To match a string with two repeated words, I would use the following Regex:
var regex = new Regex(#"^(\w+)\1$");
var result = regex.Replace(#"abcabc", #"$1");
//result: abc
^ and $ denote the start and end of the string, so that it matches only if the whole text is the repetition of two words.

C# Regex to split string by commas that are only before "or" [closed]

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
I would like to split the following string based on the commas that come before the "or" delimiter and on the "or", but not after. For example
Almondmilk, Coconutmilk or Soymilk Select Varieties, Half Gallon
becomes
Almondmilk
Coconutmilk
Soymilk Select Varieties, Half Gallon
Given your requirement as described:
var output = Regex.Split(input, "(?<!or.*),");
However, given your sample output it seems you want to split on 'or' as well:
var output = Regex.Split(input,
"((?<!or.*),)|(or)",
RegexOptions.ExplicitCapture);

Regular expression for a phrase like zzz abc_2006073122 [closed]

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
I need a regular expression for the following please
zzz abc_2006073122
zzz abc_def_2006073122
zzz abc_def_ghi_2006073122
I need to get the first 8 digits of the date (20060731) but i may have several _ parts before that so not quite sure what expression will get me what i need.
Thanks
string str = #"zzz abc_def_ghi_2006073122";
Regex reg = new Regex(#"^[a-zA-Z_ ]*_(?<someNamedGroup>\d{8})");
var match = reg.Match(str);
var result = match.Groups[1].Value;
The Regex is something like:
([0-9]{8})(?:[0-9]{2})$
Meaning: anchored to the end of the string, 8 digits to "take", 2 digits to ignore.
If you can't use the end of the line as anchor, you can try this:
(?:_)([0-9]{8})(?:[0-9]{2})
Meaning: a _ followed by 10 digits. Only 8 of these digits will be captured.

Categories

Resources