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 a string like this -
query = "UserId:(\"787D01FE-D108-4C83-A2E2-4B1DA3166A5C\" OR \"CCA47A4F-B4FA-405C-B34E-EC2E0B1F374C\") AND CreatedDate:[2017-06-20T06:14:11Z TO 2017-07-20T06:14:11Z] OR FirstName: Abc ";
But I want to get the result in array like this -
queries=
{
[0] UserId:(\"787D01FE-D108-4C83-A2E2-4B1DA3166A5C\" OR \"CCA47A4F-B4FA-405C-B34E-EC2E0B1F374C\")
[1] AND
[2] CreatedDate:[2017-06-20T06:14:11Z TO 2017-07-20T06:14:11Z]
[3] OR
[4] FirstName: Abc
}
Updates:
So far I had used this -
var result =
(from Match m in Regex.Matches(query , #"\[[^]]*]|\{[^}]*}|[^:]+")
select m.Value)
.ToArray();
But ended with this -
SOLUTION:
Based on the solution suggested by #NetMage I added some more variations to take care of double quotes, conditions inside parenthesis Here
UserId : ("787D01FE-D108-4C83-A2E2-4B1DA3166A5C" OR "CCA47A4F-B4FA-405C-B34E-EC2E0B1F374C") AND CreatedDate : [ 2017-06-20T06:14:11Z TO 2017-07-20T06:14:11Z ] AND (FirstName : "Abc" OR LastName : "Xyz")
Regex Expression -
(?:\w+?\s*:\s*(\(.+?\)|\".+?\"|\[.+?\]|\w+))|(?:\(\w+?\s*:\s*(\(.+?\)|\".*?\"*|\[.+?\]|\w+)\))|([A-Z]+( [A-Z]+ )?)
How does this work for you?
var pattern = #"(?:\w+? ?: ?(\(.+?\)|\[.+?\]|\w+))|([A-Z]+( [A-Z]+ )?)";
var ans = Regex.Matches(query, pattern).Cast<Match>().Select(m => m.Value).ToArray();
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
Hi I would like to trim a string
IWF01 - STSD Campus | 1009432 | Posted Today
I need to get this string 1009432.
Or something like this
ROS03 - Roseville, CA R-3, More... | T_R_1624621 | Posted Today
I want to get this one T_R_1624621.
How do I get that part of string only?
string s = "ROS03 - Roseville, CA R-3, More... | T_R_1624621 | Posted Today";
var strArr = s.Split('|');
string yourValue = strArr[1].Trim();
Beware, this can cause some exceptions. If you don't have right string (that can be splitted by |, or if you have string that has only one | etc...
The first thing I'd do is split the string, then I'd get the 2nd item (provided that there are enough items).
Here's a function that'll do what you need (remember that it won't raise an exception if there aren't enough items):
string GetFieldTrimmed(string input, char separator, int fieldIndex)
{
var strSplitArray = input.Split(separator);
return strSplitArray.Length >= fieldIndex + 1
? strSplitArray[fieldIndex].Trim()
: "";
}
Example usage:
var fieldVal = GetFieldTrimmed("ROS03 - Roseville, CA R-3, More... | T_R_1624621 | Posted Today", '|', 1);
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
Is there a way to split the following string (containing variables x1, x2, x3)
3x1+5x2-8x3<=9
into the following tokens
{3, 1, +5, 2, -8, 3, 9}
You can use a regular expression for that and extract only numbers with signs:
const string str = "3x1+5x2-8x3<=9";
var result = Regex.Matches(str, #"([\+\-]?[\d]+)", RegexOptions.Singleline)
.Cast<Match>().Select(x => x.Value).ToList();
Result:
[0] "3" string
[1] "1" string
[2] "+5" string
[3] "2" string
[4] "-8" string
[5] "3" string
[6] "9" string
Since the case is not as complex, I would probably solve such issue by using string.Split instead of Regex:
string str = "3x1+5x2-8x3<=9";
str = str.Replace("+", "x+").Replace("-", "x-");
string[] words = str.Split(new string[] {"x", "<=", ">=", "<", ">"}, StringSplitOptions.RemoveEmptyEntries);
The idea is to make "x" the separator apart from the inequalities. I also put "<=" and ">=" inequalities before "<" and ">". This is done to avoid "<" found before "<="
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 8 years ago.
Improve this question
In my code I get a string as input from the user and return to him a list of names(strings) that consist inside themselves the users input.
I wish to sort this list by the index of the input in the first name and last name, with priority to the first name. example:
User input string: Da
desired result:
"David Gal"
"American Dad"
"Adam Jones"
example explanation:
"David Gal" is first because the index of "Da" is 0 in the first name.
"America Dad" is second because "Da" is also first in the last name but last names have lesser priority than first names when the index is equal.
"Adam Jones" is last because "Da" has the lowest index appearance in his name.
Edit:
I've found the answer but I don't have enough reputation to answer myself so here it is:
listToSort.OrderBy(contact =>
contact.Name
.Split(' ')
.Select((WordInName, WordIndex) =>
(uint)WordInName.IndexOf(SearchString, StringComparison.CurrentCultureIgnoreCase) + WordIndex / ((double)WordIndex + 1)
)
.Min()
);
Assuming your list looks like this:
var input = new List<string>
{
"David Gal",
"American Dad",
"Adam Jones"
};
This will give you a sorted set:
var search = "da";
var results = input
.OrderBy(w =>
w.Split(' ')
.Select(x => x.ToLower().IndexOf(search)).Max());
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 am trying to get a Regex that checks to make sure that a supplied integer is 9 digits long and shall contain at least 6 non-repetitive digits
Example:
123456123 ------> Matches (6 different digits)
123243521 ------> Does not match (5 different digits)
This is much easier to do without a regex:
var str = "1234567890";
var isOk = str.Length >= 9
&& str.All(c => c >= '0' && c <= '9')
&& str.Distinct().Count() >= 6;
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
When I receive a number I want to check it against all the potential bet combinations, e.g.
L( 24, 25, 26, 27)
Would return true if I was searching for the number 24 but not 2, hope I'm clear, sorry for no code I'm a newbie.
bool ChechResult(int value)
{
String input = #"L( 24, 25, 26, 27)";
String pattern = #"\d+";
foreach (Match match in Regex.Matches(input, pattern))
{
if(value == int.Parse(match.Value))
{
return true;
}
}
return false;
}
bool foundMatch = Regex.IsMatch(subject, " " + 24 + "(?:,|\\))");