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
I need a c# regular expression for extracting the prefix of a string which has a postfix of 2 characters and a number.
I MUST USE REGEX
example:
input: "ABCDZZ4321"
output: "ABCD"
I want to cut the two 'Z' characters and the number at the end.
Another example:
input: "ABCD4R4321"
output: "ABCD"
Why bother with Regex:
var result = "ABCDZZ4321".Split('Z')[0];
EDIT:
Regex version.. even though its highly overkill:
var match = Regex.Match("ABCDZZ4321", #"^(\w+?)([A-Z0-9]{2})(\d+)$");
var result = match.Groups[1].Value; // 1 is the group.. 0 is the whole thing.
Regex is fixed now. As far as I can tell.. this will work for your requirements.
Perhaps something like this would do?
^(\w+?)\w{2}\d+$
In-depth explanation:
^ = match the beginning of the string.
\w = match any non-whitespace character(s)
\w+ = match one or more of these
\w+? = match one or more, in a "non-greedy" way (i.e. let the following match take as much as possible, which is important in this case)
\w{2} = match two non-whitespace characters
\d+ = match one or more digit character
$ = match the end of the string
(I used this site to test the regexp out while writing it.)
Also, if you only need to match A-Z, you can replace \w with [A-Z]; it seems more appropriate in that case.
You can also use this regex: (.*?ZZ) and then remove ZZ or replace whit ""
You could use ^\w{3,}\d+$. This would locate any strings that begin with at least 3 chars (2 that you need in the middle and 1 so that you have something to return) and that ends with some set of digits.
Another way is to use the string.LastIndexOf()
string input = "ABCDZZ4321";
string splitOn = "ZZ";
string result = input.Substring(0, input.LastIndexOf(splitOn));
Please try following code. I have tried with "ABCDZZ4321" and long input string in below code. In both tests it's giving required result "ABCD".
string input = "ABCDZZ455555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555321";
Regex rgx = new Regex("(?<content>.*?)[a-zA-Z]{2}[0-9]+");
Match MatchResult = rgx.Match(input);
string result = string.Empty;
while (MatchResult.Success)
{
result = MatchResult.Groups["content"].Value;
break;
}
Then even like this.
var input = "ABCDZZ4321";
var zzIndex = input.IndexOf("ZZ");
var output = input.Substring(0, zzIndex);
Regex is definitely an overengineering here
Regex.Replace(input, #"^(.+)ZZ\d+$", "$1")
Explanation:
all what comes at start of string will be catched in the group 1 (round parenthesis). In the replacement patterns it will be referenced with '$1'.
Greet you OP from the community ;)
Related
This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 3 years ago.
i'm tryng make a regex to get the string between some number and underscore, for example:
I have CP_01Ags_v5, so I need a regex to match just Ags. another example could be CP_13Hgo_v5 and match Hgo.
Some idea?
Based off the examples and matches you are describing. You want something along the lines of.
[0-9]+(.*)[_]
to break it down.
The regex looking for any number that shows up one or more times then matches everything after the number(s) up until the [_] underscore.
The downfall is this assumes the examples you provided are similar. If your example is
CP_13Hgo_v5asdf_
then it will match
Hgo_v5asdf
if you have other possible findings then you want the non-greedy version of this regex.
[0-9]+(.*?)[_]
this will cause two groups to be found in this example
CP_13Hgo_v5asdf_
will find the following groups:
Hgo
and
asdf
You can use look-arounds to match just the string between the digits and the underscore e.g.
(?<=\d)[A-Za-z]+(?=_)
Demo on regex101
In C# (note the need to escape the \ in the regex):
String s = #"CP_01Ags_v5 CP_13Hgo_v5";
Match m = Regex.Match(s, "(?<=\\d)[A-Za-z]+(?=_)");
while (m.Success) {
Console.WriteLine(m.Value);
m = m.NextMatch();
}
Output
Ags
Hgo
If your string is always at least two characters and there are no other strings of at least two characters, then you can apply the following:
var text = "CP_01Ags_v5";
var x = Regex.Match(text, #"(?<!^)[A-Za-z]{2,}");
Use Regex Group:
(?<leftPart>_\d{2})(?<YourTarget>[a-zA-Z])(?<rightPart>_[a-zA-Z0-9]{2})
C#:
Regex re = new Regex(#"(?<leftPart>_\d{2})(?<YourTarget>[a-zA-Z])(?<rightPart>_[a-zA-Z0-9]{2})");
/*
* Loop
* To get value of group you want
*/
foreach (Match item in re.Matches("CP_01Ags_v5 CP_13Hgo_v5,"))
{
Console.WriteLine(" Match: " + item.ToString());
Console.WriteLine(" Your Target you want: " + item.Groups["YourTarget"]);
}
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 am try to split the following string in to 3 parts:
Esmael20170101one => Esmael 20170101 one
What are the options?
I suggest matching instead of splitting:
string source = "Esmael20170101one";
var match = Regex.Match(source,
#"^(?<name>[A-Za-z]+)(?<code>[0-9]+)(?<suffix>[A-Za-z]{3})$");
string name = match.Groups["name"].Value;
string code = match.Groups["code"].Value;
string suffix = match.Groups["suffix"].Value;
if you insist on Regex.Split:
string[] items = Regex.Split(source, "([0-9]+)");
string name = items[0];
string code = items[1];
string suffix = items[2];
The regular expression to use is ([a-zA-Z]*)(\d+)([a-zA-Z]*)
string input = "Esmael20170101one";
var match = new Regex("([a-zA-Z]*)(\\d+)([a-zA-Z]*)").Match(input);
if (match.Success) {
Console.WriteLine(match.Groups[1].ToString());
Console.WriteLine(match.Groups[2].ToString());
Console.WriteLine(match.Groups[3].ToString());
}
Console.Read();
If you use regex, you can define what areas to capture. For example it appears that the middle component is a date, so why not specify what the date pattern is such as
^ # Beginning of String
(?<Name>[^\d]+) # Capture to `Name`
(?<Date>\d{8}) # Capture to `Date`
(?<Note>.+) # Capture to `Note`
$ # End of string
Because I have commented this you will need to use the pattern only option of IgnorePatternWhitespace which just tells the parser to strip the comments (#) out.
The result will be this in a single match
Group[0] has the whole thing matched.
Group["Name"] or Group[1] is the name that is found.
Group["Date"] or Group[2] is the date that is found.
Group["Note"] or Group[3] is the note which is found.
As Dmitry pointed out, we need more information. All of these patterns can fail if there are numbers found in either of the groups depending on their location. If you know that all dates are within the 21st century adjust my pattern to be (?<Date>20\d{6}) to make sure that a true date is captured in that field; though it is not foolproof.
This question already has an answer here:
Learning Regular Expressions [closed]
(1 answer)
Closed 7 years ago.
I have a string of different emails
ex: "email1#uy.com, email2#iu.it, email3#uu.edu" etc, etc
I would like to formulate a Regex that creates the following output
ex: "email1,email2,email3" etc, etc
How can I remove characters between an "#" and "," but leaving a "," and a Space in C#
Thank you so much for the help!!
If you want to replace all characters between # and comma by blank, the easiest option is to use Regex.Replace:
var emails = "a#m.com, b#m.com, d#m.com";
var result = Regex.Replace(emails, "#[^,]+", string.Empty);
// result is "a, b, d"
Please note that it leaves spaces after comma in the result, as you wanted in your question, though your example result has spaces removed.
The regular expression looks for all substrings starting '#' characters, followed by any character which is not comma. Those substrings are replaced with empty string.
Replacing all occurrences of #[^,]+ with an empty string will do the job.
The expression matches sequences that start in #, inclusive, up to a comma or to the end, exclusive. Therefore, commas in the original string of e-mails would be kept.
Demo.
Maybe you don't need to use a regex, in that case you can do the following:
string input = "email1#uy.com, email2#iu.it, email3#uu.edu";
input = input.Replace(" ", "");
string[] ocurrences = input.Split(',');
for (int i = 0; i < ocurrences.Length; i++)
{
string s = ocurrences[i];
ocurrences[i] = s.Substring(0, s.IndexOf('#'));
}
string final = string.Join(", ", occurences);
String pattern = #"^(\d{11})$";
String input = "You number is:11126564312 and 12234322121 \n\n23211212345";
Match match = Regex.Match(input,pattern);
From the above code I am planning to capture the 11 digit strings present in above text but match.Success is always returning false. Any ideas.
This is because you have used ^ and $.
Explaination: The meaning of your regular expression is "match any string that contains exactly 11 digits from start to end". The string You number is:11126564312 and 12234322121 \n\n23211212345 is not a string like that. 01234567890 is like that string.
What you need: You need regular expression for match any string that contains exactly 11 digits. start to end is omitted. ^ and $ is used for this. So you need this regex.
String pattern = #"(\d{11})";
As the sub-pattern to capture contains the whole regex you dont need () at all. Just the regex ill do.
String pattern = #"\d{11}";
String pattern = #"^(\d{11})$";
String input = "11126564312"
Match match = Regex.Match(input,pattern);
will pass.
Your Regex specify it has to be 11 numbers ONLY
^ = starts with
$ = ends with
if you want to check if it contains 11 numbers change the regex to
String pattern = #"\d{11}";
Your Regex matches a string that has exactly 11 digits, but no text before, between or after. That is why you don't get any matches here.
To match 11 digits anywhere in the string, simply use:
string pattern = #"\d{11}";
I have a little problem on RegEx pattern in c#. Here's the rule below:
input: 1234567
expected output: 123/1234567
Rules:
Get the first three digit in the input. //123
Add /
Append the the original input. //123/1234567
The expected output should looks like this: 123/1234567
here's my regex pattern:
regex rx = new regex(#"((\w{1,3})(\w{1,7}))");
but the output is incorrect. 123/4567
I think this is what you're looking for:
string s = #"1234567";
s = Regex.Replace(s, #"(\w{3})(\w+)", #"$1/$1$2");
Instead of trying to match part of the string, then match the whole string, just match the whole thing in two capture groups and reuse the first one.
It's not clear why you need a RegEx for this. Why not just do:
string x = "1234567";
string result = x.Substring(0, 3) + "/" + x;
Another option is:
string s = Regex.Replace("1234567", #"^\w{3}", "$&/$&"););
That would capture 123 and replace it to 123/123, leaving the tail of 4567.
^\w{3} - Matches the first 3 characters.
$& - replace with the whole match.
You could also do #"^(\w{3})", "$1/$1" if you are more comfortable with it; it is better known.
Use positive look-ahead assertions, as they don't 'consume' characters in the current input stream, while still capturing input into groups:
Regex rx = new Regex(#"(?'group1'?=\w{1,3})(?'group2'?=\w{1,7})");
group1 should be 123, group2 should be 1234567.