Analysis of parameters in Java written test (regular matching)

Source: Internet
Author: User

Enter the following command at the command line:

xcopy/s c \ d:\,

Each parameter is as follows:

Parameter 1: command-word xcopy

Parameter 2: string/S

Parameter 3: string \ c \

Parameter 4: String d:\

Please write a parameter resolver to parse the command line parameters.

Parsing rules:

1. Parameter delimiter is a space
2. For parameters that are included with "", if there is a space in the middle, it cannot be resolved to more than one parameter. For example, in the command line input xcopy/s "C:\program files" "D:\", the parameters are still 4, the 3rd parameter should be the string C:\program files, not C:\program, pay attention to the output parameters, you need to "" "Remove, The quotation marks do not exist in the nested condition.
3. Indefinite length of parameters
4. Input is guaranteed by use case, no non-conforming input is present

Links: https://www.nowcoder.com/questionTerminal/668603dc307e4ef4bb07bcd0615ea677
Source: Niu Ke Net

Input Description:

Enter a line of string, you can have spaces



Output Description:

Number of output parameters, decomposed parameters, each of which is exclusive of one row

Input Example:
XCOPY/S c:\\ D:\

Output Example:
4xcopy/sc:\d:\\

 Packagetest;Importjava.util.ArrayList;ImportJava.util.Scanner;ImportJava.util.regex.Matcher;ImportJava.util.regex.Pattern;//using regular Expressions Public classEXAM19 { Public Static voidMain (string[] args) {Scanner Scanner=NewScanner (system.in); //result stores the string after the regular expression is determinedarraylist<string> result =NewArraylist<string>(); String Reg= "(\".*?\")| ([a-za-z_0-9:/\\\\]+) ";//* Non-greedy match, minimum repetition;/\\\\ to match \ \Pattern pattern =Pattern.compile (REG);  while(Scanner.hasnext ()) {string string=Scanner.nextline (); //get Matcher ObjectMatcher Matcher =Pattern.matcher (string); String tokestring= ""; intpos = 0;  while(Pos <string.length ()) {                //match positioning: start to endmatcher.region (POS, String.Length ()); if(Matcher.lookingat ()) {//Group (0) denotes the entire string;//Group (1) matches only the first parenthesis;//Group (2) matches the second bracket only                    if(Matcher.group (1)! =NULL) {Result.add (Matcher.group (1). ReplaceAll ("\" "," ")); } Else if(Matcher.group (2)! =NULL) {Result.add (Matcher.group (2)); } POS= Matcher.end ();//Final index of total match}Else{pos++;            }} System.out.println (Result.size ());  for(String tok:result) {System.out.println (tok);    }} scanner.close (); }}

Analysis of parameters in Java written test (regular matching)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.