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.
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 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 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 5 years ago.
Improve this question
I'm learning about Regex in C#, I've written the codes to check a valid day but Regex.Match is always false.
var pat = #"(today|tomorrow|yesterday)((?:\s*[+-]?\s*\d+\s*[dwym])*)"; // Can not change
Match match = Regex.Match(value, pat);
the match.Success is always false (value = 2017-07-07)
Which format can match with that pattern?
What are you actually trying to achieve? From the looks of it you are trying to match either some text (today/tomorrow/yesterday), or a date in a specific format?
Try this:
(today|tomorrow|yesterday|\d{4}-\d{2}-\d{2})
Try using a regular expression designer as well. I use Rad Software Regular Expression Designer 1.4.
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
I was trying regular expression
to accept only numbers and it should not allow to accept more than ten
^[]0,10]{0,10}$
This matches just this range of numbers: [0-9]
^([0-9])$
And then you can use $1 if you need to replace is with something else ...
Or also you can use \d instead of [0-9] like this:
^(\d)$
And if you want to accept 10, you have to use |. Some thing like this;
^(\d|10)$
As I said, this ^ regex accept both this range [0-9] and 10.
You can try this one:
^[0-9]{0,10}$
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 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);