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.
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 4 years ago.
Improve this question
I have string like (C1&&C2)
I need to split this by using (,&&,) and output should be an array like..
(
C1
&&
C2
)
... tried string.split and regex.split but didn't get the expected output.
You could use Regex:
string input = "(C1&&C2)";
List<string> output = new List<string>();
foreach(Match m in Regex.Matches(input,#"(\w+|\W+)"))
{
output.Add(m.Value);
}
Or even simple:
string input = "(C1&&C2)";
string[] output = Regex.Split(input,#"\b");
Try this regex pattern: ^(\()([A-Z0-9]*)(&&)([A-Z0-9]*)(\))
It returns a set of captured groups ;)
Explanation:
^(\() - captures literal (
([A-Z0-9]*) - captures any character from range with any nyumber of repetitions...
(&&) - captures literal &&
(\)) - captures literal )
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 4 years ago.
Improve this question
I want to create regex for following scenario
name like %name -- match and extract %name
name like '%name' -- should not match as it has ' before %
name like ' %name' -- should not match as previous character is ' ( ignoring space )
name like%name match and extract %name
This is what I got [^']%\w+ but this dosen't cover last two cases
This may suit your needs, although it does depend on the string being prefixed with name like.
void Main()
{
var strings = new string[] {"name like %name", "name like '%name'", "name like ' %name'", "name like%name"};
var regex = new Regex("^name like ?(%\\w+)");
foreach (var item in strings)
{
Console.WriteLine($"string: \"{item}\" matches: {regex.IsMatch(item)} extracted: {(regex.Match(item).Groups.Count > 0 ? regex.Match(item).Groups[1].Value : string.Empty)}");
}
//Output:
//string: "name like %name" matches: True extracted: %name
//string: "name like '%name'" matches: False extracted:
//string: "name like ' %name'" matches: False extracted:
//string: "name like%name" matches: True extracted: %name
}
The regex breakdown is:
^name like: a string starting with name like
?: an optional space
(%\\w+): our match group, picking any number of characters that are in [a-zA-Z_] prefixed with %
This question already has answers here:
How do I extract text that lies between parentheses (round brackets)?
(19 answers)
Closed 7 years ago.
As I know for selecting a part of a string we use split. For example, if node1.Text is test (delete) if we choose delete
string b1 = node1.Text.Split('(')[0];
then that means we have chosen test, But if I want to choose delete from node1.Text how can I do?
Update:
Another question is that when there are two sets of parenthesis in the string, how one could aim at delete?. For example is string is test(2) (delete) - if we choose delete
You can also use regex, and then just remove the parentheses:
resultString = Regex.Match(yourString, #"\((.*?)\)").Value.
Replace("(", "").Replace(")", "");
Or better:
Regex.Match(yourString, #"\((.*?)\)").Groups[1].Value;
If you want to extract multiple strings in parentheses:
List<string> matches = new List<string>();
var result = Regex.Matches(yourString, #"\((.*?)\)");
foreach(Match x in result)
matches.Add(x.Groups[1].Value.ToString());
If your string is always xxx(yyy)zzz format, you can add ) character so split it and get the second item like;
var s = "test (delete) if we choose delete";
string b1 = s.Split(new[] { '(', ')' })[1];
string tmp = node1.Text.Split('(')[1];
string final = tmp.Split(')')[0];
Is also possible.
With the index [x] you target the part of the string before and after the character you have split the original string at. If the character occurs multiple times, your resulting string hat more parts.
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'm trying to capture every words in a string that are inside an array, and I also want to capture every word that isn't (basically splitting the string by the words that I have and the one that I don't).
For clarification, here's an example:
Input:
string myString = "ID-NUMthatissomeboringstuffNAME"
string[] wordsList = new string[] { "ID", "NUM", "NAME" };
Desired output captures (in order):
(ID)
(-)
(NUM)
(thatissomeboringstuff)
(NAME)
My regex:
string regex = String.Format("(({0})|.*)", String.Join("|", rules))
Of course it doesn't work, when I tried to replace, it would just give me the first letter caught.
Once more, my original problem is already solved (I just wanted to parse a string like that and put some value depending on what I found in it... A mere Replace suffice :p), but for the sake of completeness, I'd like to solve it this way anyway.
This should do it ({0}|[^({0})]+). Here's the Regexr breakdown.
Code
var input = "ID-NUMthatissomeboringstuffNAME";
string[] wordsList = new string[] { "ID", "NUM", "NAME" };
string regex = String.Format( "({0}|[^({0})]+)", String.Join( "|", wordsList ) );
foreach(var match in Regex.Matches( input, regex )
.OfType<Match>()
.Select( match => match.Captures[0].Value ) ){
Console.WriteLine( match );
}
indexOf(string)
gives you the position of the string and the you can use
substring(indexStart, word length)
to get it
I am not sure that this is more readable, but you could use this regex:
#"(?<g0>[A-Z]*)(?<g1>[^A-Z]*)(?<g2>[A-Z]*)(?<g3>[^A-Z]*)(?<g4>[A-Z]*)"
It does what you find, finds groups of uppercase letters, then non-uppercase letters, etc, and stores them in named groups.
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 ;)