Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I am integrating Lua in my application. But I am getting error if I am adding any if condition in Expression
string ifCondition =" return 10 + 1";
string expression = "function f()\n" + ifCondition + " \nend";
state.DoString(expression);
var scriptFunc = state["ScriptFunc"] as LuaFunction;
var res = scriptFunc.Call();
Response.Write(res[0]);
If I run above code then it will work. now I am adding if condition then it will not work.
Here is my code
string ifCondition ="If (10 < 2) then \n return 10 + 1 \n else \n return 12 end";
string expression = "function f()\n" + ifCondition + " \nend";
state.DoString(expression);
var scriptFunc = state["ScriptFunc"] as LuaFunction;
var res = scriptFunc.Call();
Response.Write(res[0]);
How can I execute if condition dynamically?
string ifCondition ="If (10 < 2) then \n return 10 + 1 \n else \n return 12 end";
Lua keywords are all lower case. Change the If to if.
By the way, C# supports multiline strings, so you could write the above as:
string ifCondition = #"
if (10 < 2) then
return 10 + 1
else
return 12
end";
Not such a big deal here, but if your snippets get longer it'll get harder to maintain them as a single line.
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 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();
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 need to convert this function from C# to VB.NET for use in SSRS report.
This function should take a number/INT and return a multi character string.
So 26 would be simple Z
but 27 would = AA
78 = AAA
79 = AAB
and so on
Function to convert:
public static String getColumnNameFromIndex(int column)
{
column--;
String col = Convert.ToString((char)('A' + (column % 26)));
while (column >= 26)
{
column = (column / 26) -1;
col = Convert.ToString((char)('A' + (column % 26))) + col;
}
return col;
}
If you're being LAZY and i mean lazy you can use http://converter.telerik.com/
Here's the output for you:
Public Shared Function getColumnNameFromIndex(column As Integer) As String
column -= 1
Dim col As String = Chr(Asc("A") + (column Mod 26))
While column >= 26
column = (column \ 26) - 1
col = Chr(Asc("A") + (column Mod 26)) & col
End While
Return col
End Function
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
I have the following string that is captured from the DVLA when looking up a car registration details and I need to be able to extract just the numbers from the CC.
"A5 S LINE BLACK EDITION PLUS TDI 190 (2 DOOR), 1968cc, 2015 -
PRESENT"
Given that the lentgh of the string can change, is there a way to do this with a sub-string so for example always grab the numbers from before the cc without the space that comes before it? Bare in mind too that this can sometimes be a 3 digit number or a four digit number.
This does the trick:
string input = "A5 S LINE BLACK EDITION PLUS TDI 190 (2 DOOR), 1968cc, 2015 - PRESENT";
string size;
Regex r = new Regex("(\\d*)cc", RegexOptions.IgnoreCase);
Match m = r.Match(input);
if (m.Success)
{
size = m.Groups[0];
}
It captures every number that is right before cc
If the count of the comma doesn't change you can do following:
string s = "A5 S LINE BLACK EDITION PLUS TDI 190 (2 DOOR), 1968cc, 2015 - PRESENT";
string ccString = s.Split(',').ToList().Find(x => x.EndsWith("cc")).Trim();
int cc = Int32.Parse(ccString.Substring(0, ccString.Length - 2));
You can use Regex to match a pattern withing the string - so you can return parts of the string that match the given pattern. This Regex pattern will attempt to match parts of the string that fit the following pattern:
\d{1,5} *[cC]{2}
Starts with 1 to 5 digits \d{1,5} (seems sensible for an engine cc value!)
Can then contain 0 or more spaces in between that and cc *
Ends with any combination of 2 C or c [cC]{2}
So you can then use this in the following manner:
string str = "A5 S LINE BLACK EDITION PLUS TDI 190 (2 DOOR), 1968cc, 2015 - PRESENT";
Match result = Regex.Match(str, #"\d{1,5} *[cC]{2}");
string cc = result.Value; // 1968cc
Here is another solution:
string text = "A5 S LINE BLACK EDITION PLUS TDI 190 (2 DOOR), 1968cc, 2015 - PRESENT";
string[] substrings = text.Split(',');
string numeric = new String(substrings[1].Where(Char.IsDigit).ToArray());
Console.WriteLine(numeric);
Here is a working 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 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;