0

I am making a personal terminal, it doesn't do a lot and doesn't really have a specific purpose, I'm mostly just bored. I have been banging my head against a wall for about a day now trying to do this manually before trying to research it.

When I did research it I found this and changed it a bit to apply to my code

var re = new Regex("(?<=\")[^\"]*(?=\")|[^\" ]+");
string[] NewTokens = re.Matches(fullstring).Cast<Match>().Select(m => m.Value).ToArray();

The fullstring variable is all of my tokens clumped together (basically just the user input for a command), not seperated by spacing. Just the normal string before it is deconstructed into a string[] of tokens.

I need this string to be seperated by spaces but ignore that when there are quotes. For example if I wanted to make a command that prints out a message into the console I would do: print "Hello World!" as my command. The output I would like is:

[0] -> print
[1] -> Hello World!

The code example seems to work...but when I try to run a command that doesnt have quotes like:
(print being the command the help command looks for)
help print

The output is:
[0] -> helpprint

The tokens should be:
[0] -> help [1] -> print

Please help. Normally I wouldn't ask for help, but I really can't find a fix for this online or by myself.

bill
  • 21
  • 3
  • Does this answer your question? [Regex for splitting a string using space when not surrounded by single or double quotes](https://stackoverflow.com/questions/366202/regex-for-splitting-a-string-using-space-when-not-surrounded-by-single-or-double) – 41686d6564 stands w. Palestine Sep 06 '22 at 22:26
  • `Regex.Matches(input, @"[^\s""]+|""([^""]*)""").Cast().Select(m => m.Groups[1].Success ? m.Groups[1].Value : m.Value);` should do the trick. – 41686d6564 stands w. Palestine Sep 06 '22 at 22:34
  • @41686d6564standsw.Palestine I have already seen that and unfortunately that solution is for java not C#. – bill Sep 07 '22 at 08:14
  • @41686d6564standsw.Palestine That second solution code doesnt really work as it returns a IEnumerable. I need it to be a string[] for processing. – bill Sep 07 '22 at 08:21
  • @41686d6564standsw.Palestine I slapped a .ToArray() at the end of `Regex.Matches(input, @"[^\s""]+|""([^""]*)""").Cast().Select(m => m.Groups[1].Success ? m.Groups[1].Value : m.Value)` and it spit out a string[] but it doesnt seem to act differently from my previous method. – bill Sep 07 '22 at 08:33
  • I checked both your samples (`print "Hello World!"` and `help print`) with the pattern `(?<=\")[^\"]*(?=\")|[^\" ]+` and everything looks fine. – Persian Brat Jan 21 '23 at 15:09

1 Answers1

-1

Just change the Select this way to return the matched group

string[] NewTokens = re.Matches(fullstring).Select(m => m.Groups[0].Value).ToArray();
jacdDev
  • 196
  • 1
  • 8
  • This results in an error where "Matches(fullstring) does not contain .Select". Not the exact error message. visual studio wouldnt let me copy-paste. – bill Sep 07 '22 at 08:25
  • Maybe you are missing Linq, you can add `using System.Linq;` and it should let you use the `Select` function – jacdDev Sep 07 '22 at 20:36