Getting all the items in an array except one of my choice - c#

Im trying to make a bot that will recognize different commands. For example, one command would be " add 'x text' " , as a TODO list. I have set up the recognition of the command by splitting the input to an array, and then getting array[0], or else the first word which will always be the command. Now, I would like to somehow save the other items in the sentence that was split except from the keyword (AKA [0] ) , meaning [1] [2] [3] etc. What is one way of getting all the items besides the [0] one ? I've tried to skip it by doing: myarray.Skip(x) , but I failed at making it work. Thanks for all your suggestions!

As #L-Three has suggested, you can use string.Join(" ", myarray.Skip(1).ToArray()) to get the rest of the string without the first word.
Your example with myarray.Skip(x) didn't work because Skip(x) is a Linq function and thus it is a deferred execution operation. It means that you just described what you need to do, but the action isn't executed yet. A call to '.ToArray()', '.ToList()' for example (as L-Three has used in his solution) will execute the action.
It is important to note that there are other ways to achieve what you want (like using Regex - as it is already suggested in the comments) which might be safer than the method that you are using now.

Related

Search for statement not followed by other

I have a code like this:
myType.DoSomething(myParams);
That should immediately be followed by this statement
anotherType.DoeSomethingDifferent(otherParams);
Is there a way on ReShaprer I can identify all those sources where the first statement is NOT followed by the second one? I tried to use the "Search With Pattern" command with the following:
$aVar$.DoSomething($args$)
However this only lists all usages of exactly that method on myType. I need those usages that arte not followed by the second statement. Is this possible?
EDIT: Of course I could simply add the two lines into one single method. However there MIGHT be scenarios in which writing the first line without the second one is usefull. I simply want to grab those.

NSDK Skip instruction?

Sorry in advance for the inappropriate tag (this is more a NSDK issue than a C# issue, but NSDK tag wasn't existing, and I couldn't create it so I had to choose one...)
I'm currently writting a webservice using C#.
My goal is to reproduce what a NSDK code is doing.
Everything is going ok so far but one thing.
I have absolutely no clue about what the skip instruction is doing.
Here is an exemple of an instruction
if skip SomeString <> ''
I know this is testing if someString is empty or not, but the "skip" makes me wonder what it does.
The main goal of my webservice is to create a file, and to send it to a printer after a writing phase and I need to be vary careful with spaces or backlines and stuff so if someone could explain this to me, I'd gladly appreciate!
Best Regards.
Got my Answer.
skip instruction is removing spaces at the beginning and the end of the string.
For instance :
VALUE$ = "  key "
There is 2 spaces before and 1 space after the value.
length(VALUE$) gives 6 (the 3 spaces + the three letters)
length(skip VALUE$) gives 3 (only the three letters)
So skip VALUE$ equals "key", and not "  key "

Regex between, from the last to specific end

Today my wish is to take text form the string.
This string must be, between last slash and .partX.rar or .rar
First I tried to find edge's end of the word and then the beginning. After I get that two elements I merged them but I got empty results.
String:
http://hosting.xyz/1234/15-game.part4.rar.html
http://hosting.xyz/1234/16-game.rar.html
Regex:
Begin:(([^/]*)$) - start from last /
End:(.*(?=.part[0-9]+.rar|.rar)) stop before .partX.rar or .rar
As you see, if I merge that codes I won't get any result.
What is more, "end" select me only .partX instead of .partX.rar
All what I want is:
15-game.part4.rar and 16-game.rar
What i tried:
(([^/]*)$)(.*(?=.part[0-9]+.rar|.rar))
(([^/]*)$)
(.*(?=.part[0-9]+.rar|.rar))
I tried also
/[a-zA-Z0-9]+
but I do not know how select symbols.. This could be the easiest way. But this select only letters and numbers, not - or _.
If I could select symbols..
You don't really need a regex for this as you can merely split the url on / and then grab the part of the file name that you need. Since you didn't mention a language, here's an implementation in Perl:
use strict;
use warnings;
my $str1="http://hosting.xyz/1234/15-game.part4.rar.html";
my $str2="http://hosting.xyz/1234/16-game.rar.html";
my $file1=(split(/\//,$str1))[-1]; #last element of the resulting array from splitting on slash
my $file2=(split(/\//,$str2))[-1];
foreach($file1,$file2)
{
s/\.html$//; #for each file name, if it ends in ".html", get rid of that ending.
print "$_\n";
}
The output is:
15-game.part4.rar
16-game.rar
Nothing could be simpler! :-)
Use this:
new Regex("^.*\/(.*)\.html$")
You'll find your filename in the first captured group (don't have a c# compiler at hand, so can't give you working sample, but you have a working regex now! :-) )
See a demo here: http://rubular.com/r/UxFNtJenyF
I'm not a C# coder so can't write full code here but I think you'll need support of negative lookahead here like this:
new Regex("/(?!.*/)(.+?)\.html$");
Matched Group # 1 will have your string i.e. "16-game.rar" OR "15-game.part4.rar"
Use two regexes:
start to substitute .*/ with nothing;
then substitute \.html with nothing.
Job done!

Efficient means for parsing and replacing values in T-SQL

I have a lot of T-SQL queries I need to parse and replace certain values.
A query may look like this
SELECT dbo.udfTest(123,'Bob') as Foo
or alternatively
SELECT dbo.udfTest(123) as Foo
My task is to replace the number value with another given value but as I contemplate just rolling something up using the string class and doing substrings etc I start to run into lots of edge cases like this
SELECT dbo.udfTest ( 123 ) as Foo
or
SELECT [dbo].[udfTest]( 123 ) as Foo
or
SELECT [dbo].[udfTest]( 123 ) as Foo1, dbo.udfTest(123) as Foo2
Throw in any combination of whitespace, casing, brackets, nested parenthesis and you can imagine the number of variations I would have to cover...nasty.
Which brings me to wondering if there is a better way? RegEx may be a play but I figured I would toss it out to get some opinions.
You might be able to use the database features of Visual Studio. See API Reference for Database Features of Visual Studio, especially the Microsoft.Data.Schema.ScriptDom Namespace.
Also, a quick search for "parse mdx query" turned up several interesting hits. I'm pretty sure I once found a tool that could parse MDX queries, then use the parse tree to create a formatted version.
Perhaps the article Getting to the Crown Jewels will help. If nothing else, it may give you a hint about who to ask for help.
Matching multiple possible patterns says RegEx right off the bat. The RegEx will be ugly but it should get you where you want to go pretty easily.
If the end goal is to replace some set of numbers with a new set of numbers (basically a translation), wouldn't it be easier update your UDF to use a lookup table to map the old values to the new ones instead of trying to intercept every method call before it takes place? (Unless your examples have been greatly simplified in that they only show the same function being called each time, but in reality there's many that would have to be updated)

Regex to remove xml declaration from a string

First of all, I know this is a bad solution and I shouldn't be doing this.
Background: Feel free to skip
However, I need a quick fix for a live system. We currently have a data structure which serialises itself to a string by creating "xml" fragments via a series of string builders. Whether this is valid XML I rather doubt. After creating this xml, and before sending it over a message queue, some clean-up code searches the string for occurrences of the xml declaration and removes them.
The way this is done (iterate every character doing indexOf for the <?xml) is so slow its causing thread timeouts and killing our systems. Ultimately I'll be trying to fix this properly (build xml using xml documents or something similar) but for today I need a quick fix to replace what's there.
Please bear in mind, I know this is a far from ideal solution, but I need a quick fix to get us back up and running.
Question
My thought to use a regex to find the declarations. I was planning on: <\?xml.*?>, then using Regex.Replace(input, string.empty) to remove.
Could you let me know if there are any glaring problems with this regex, or whether just writing it in code using string.IndexOf("<?xml") and string.IndexOf("?>") pairs in a (much saner) loop is better.
EDIT
I need to take care of newlines.
Would: <\?xml[^>]*?> do the trick?
EDIT2
Thanks for the help. Regex wise <\?xml.*?\?> worked fine. I ended up writing some timing code and testing both using ar egex, and IndexOf(). I found, that for our simplest use case, JUST the declaration stripping took:
Nearly a second as it was
.01 of a second with the regex
untimable using a loop and IndexOf()
So I went for IndexOf() as it's easy a very simple loop.
You probably want either this: <\?xml.*\?> or this: <\?xml.*?\?>, because the way you have it now, the regex is not looking for '?>' but just for '>'. I don't think you want the first option, because it's greedy and it will remove everything between the first occurrence of ''. The second option will work as long as you don't have nested XML-tags. If you do, it will remove everything between the first ''. If you have another '' tag.
Also, I don't know how regexes are implemented in .NET, but I seriously doubt if they're faster than using indexOf.
strXML = strXML.Remove(0, sXMLContent.IndexOf(#"?>", 0) + 2);

Categories

Resources