1

I am trying create a basic file system to imitate the terminal. I am currently stuck on getting the names after the command. My first thought was to use regex to parse the commands.

Examples of commands would be:

mkdir hello
ls
cd hello

However to account for many whitespaces an input could be mkdir hello. I was wondering if there is another way without using regex? Also I was curious to which method is faster? With or without regex?

3 Answers 3

1

You could try splitting the lines like

String[] tokens = line.split(" ");

And for basic commands, most likely your command will be at tokens[0] followed by arguments.

Sign up to request clarification or add additional context in comments.

1 Comment

Don't forget to accept the answer that is most helpful to you. Thanks!
1
for(String current: line.split("\\s+"){
   //do something.
}

Comments

1

Usually, regex is faster because you can compile it.

see java.util.regex - importance of Pattern.compile()?

(Internally, I think the JVM always compile the regex at some point, but if you do it explicitly, you can reuse it. I am not sure if the JVM is smart enough to reuse a compiled regex locally, maybe it is)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.