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.
Related
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 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 4 years ago.
Improve this question
Need to write a regular expression which would match all the files having names like:
ABCD^^A12397_4^^A12397_5^^ABCServer2^^1^^20190117654345
ABCDE^^A12394567_4^^A165557_5^^ABCServer5^^3^^20180117654345
Basically need to match pattern like :
Alphabets^^AlphaNumericWithSpecialChracter^^ANWSC^^ANWSC^^Numeric^^Numeric
Things i have tried :
Regex.IsMatch("ABCD^^A12397_4^^A12397_5^^Server2^^1^^20190117654345", "^ABCD\\^\\^A12397_4\\^\\^A12397_5\\^\\^Server2\\^\\^1\\^\\^20190117654345$");
Thanks .
Try this pattern: ^[A-Za-z](\^\^[\w_]+){3}(\^\^\d+){2}$
Explanation:
[A-Za-z] = Alphabets
(\^\^[\w_]+){3} = ^^AlphaNumericWithSpecialChracter repeated three times (with special character _
(\^\^\d+){2} = ^^Numeric repeated twice
Let's build the regular expression step by step:
Alphabets^^AlphaNumericWithSpecialChracter^^ANWSC^^ANWSC^^Numeric^^Numeric
Assuming
Alphabets - One or more A..Z or a..z letters - [A-Za-z]+
AlphaNumericWithSpecialChracter - One or more A..Z, a..z, 0..9 letters or _ - [A-Za-z0-9_]+
ANWSC - One or more A..Z, a..z, 0..9 letters - [A-Za-z0-9]+
Numeric - One or more 0..9 letters - [0-9]+
Now we should start the pattern with ^ anchor, end it with $ one and combine the chunks with \^\^ (please, note, escapement: we want ^ as a plain character, not as an anchor):
string pattern =
#"^[A-Za-z]+\^\^[A-Za-z0-9_]+\^\^[A-Za-z0-9]+\^\^[A-Za-z0-9]+\^\^[0-9]+\^\^[0-9]+$";
Or if we want to add readability we can put it as
string pattern = string.Join(#"\^\^",
"^" + // String start
"[A-Za-z]+", // Alphabets
"[A-Za-z0-9_]+", // AlphaNumericWithSpecialChracter
"[A-Za-z0-9]+", // ANWSC
"[A-Za-z0-9]+", // ANWSC
"[0-9]+", // Numeric
"[0-9]+" + // Numeric
"$"); // End of 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 5 years ago.
Improve this question
I have this line:
abc cd_ef_1g fff
the 'abc' is a fixed value and always there. The fff can change.
I'm now searching for the following word, which is always second, always between whitespaces or tabs, and always after 'abc'.
cd_ef_1g
It could be with '_' or '-', or numbers.
I've searched all over, found similar results but not exactly with these conditions (using REGEX in C#).
Please help.
According to your description, this should be sufficient:
abc\s+([\w-]+)\s+fff
abc # Always before
\s+ # One or more white-space chars
([\w-]+) # One or more alphanumeric chars ('-' also included)
\s+ # One or more white-space chars
fff # Always after
The captured group is the word sought after.
Demo
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
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.