2014-04-01 110 views
0

例如,輸入將是這樣的:如何獲得雙引號之間的字符串,字符串中的Java中

AddItem rt456 4 12 BOOK 「File Structures」 「Addison-Wesley」 「Michael Folk」 

,我想用掃描儀讀取所有,並把它放在一個陣列英寸

喜歡:

info[0] = rt456 
    info[1] = 4 
    .. 
    .. 
    info[4] = File Structures 
    info[5] = Addison-Wesley 

所以,我怎麼能得到引號之間的字符串?

編輯:我的代碼 - >

public static void main(String[] args) { 
      String command; 
     String[] line = new String[6]; 
     Scanner read = new Scanner(System.in); 
     Library library = new Library(); 

     command = read.next(); 

     if(command.matches("AddItem")) 
     { 
      line[0] = read.next(); // Serial Number 
      line[1] = read.next(); // Shelf Number 
      line[2] = read.next(); // Shelf Index 
      command = read.next(); // Type of the item. "Book" - "CD" - "Magazine" 

      if(command.matches("BOOK")) 
      { 
       line[3] = read.next(); // Name 
       line[4] = read.next(); // Publisher 
       line[5] = read.next(); // Author 

       Book yeni = new Book(line[0],Integer.parseInt(line[1]),Integer.parseInt(line[2]),line[3],line[4],line[5]); 


    } 
    } 
} 

所以我用read.next沒有引號來讀取字符串的一部分。

解決的使用正則表達式AS

read.next("([^\"]\\S*|\".+?\")\\s*"); 
+0

閱讀本書之後,將分隔符更改爲'「'或者使用一個使用空格作爲分隔符的CSV解析器 –

+1

'新的StreamTokenizer(新的StringReader(mystring))'應該有所訣竅 –

+0

是字段的數量,這些字段的位置每次都一樣嗎? – JohnnyAW

回答

0

使用凌亂的正則表達式的替代:

public static void main(String[] args) throws Exception { 
    Pattern p = Pattern.compile("^(\\w*)[\\s]+(\\w*)[\\s]+(\\w*)[\\s]+(\\w*)[\\s]+(\\w*)[\\s]+[「](.*)[」][\\s]+[「](.*)[」][\\s]+[「](.*)[」]"); 
    Matcher m = p.matcher("AddItem rt456 4 12 BOOK 「File Structures」 「Addison-Wesley」 「Michael Folk」"); 

    if (m.find()) { 
     for (int i=1;i<=m.groupCount();i++) { 
      System.out.println(m.group(i)); 
     } 
    } 
} 

,打印:

AddItem 
rt456 
4 
12 
BOOK 
File Structures 
Addison-Wesley 
Michael Folk 

我認爲行情是因爲你在問題「」,而不是「」類型的他們,所以他們不需要逃脫。

+1

可以解釋你的正則表達式。哦,它已經被接受爲答案。所以用戶會複製粘貼W/O知道代碼在做什麼 – spiderman

+0

我也覺得正則表達式是這樣做的方式,可能是因爲我是StreamTokenizer的新手 – spiderman

+0

幾個字母數字組\ w與他們之間有一個或多個空格\ s +和三組[「](。*)[」]引用,char組引用。它不是神祕莫測的。它是逐組列出的。它可能會做得更好,更簡單 –

-1

你可以試試這個。我已經準備了演示您的需求

public static void main(String args[]) { 
     String str = "\"ABC DEF\""; 
     System.out.println(str); 
     String str1 = str.replaceAll("\"", ""); 
     System.out.println(str1); 
    } 

閱讀只是更換空字符串

+0

This would不產生OP描述的輸出。 OP想要將引用的字符串解析爲單個標記,而不是去掉引號。查看示例所需的輸出。 –

1

雙引號作爲參考後,看看這個:Scanner Docs

你如何從掃描儀讀取取決於您如何向用戶展示數據。

如果他們打字都放在同一行:

Scanner scanner = new Scanner(System.in); 
String result = ""; 
System.out.println("Enter Data:"); 
result = scanner.nextLine(); 

否則,如果你把它分解成輸入字段,你可以這樣做:

Scanner scanner = new Scanner(System.in); 
System.out.println("Enter Identifier:"); 
info[0] = scanner.nextLine(); 
System.out.println("Enter Num:"); 
info[1] = scanner.nextLine(); 
... 

如果你想分配之前驗證什麼數據到一個變量,嘗試使用scanner.next("");其中報價包含匹配的正則表達式模式

編輯:

檢查here爲正則表達式信息。

作爲一個例子,說我有一個字符串

String foo = "The cat in the hat"; 

正則表達式(正則表達式),可以使用在一個非常快速和有效的方式來操作此字符串。如果我採用該字符串並執行foo = foo.replace("\\s+", "");,則這將替換任何空白的空白,因此消除空白。

打破論點\\s+,我們有\s這意味着匹配任何字符是空白。

\s之前的額外\是一個轉義字符,它允許\s被正確讀取。

+表示匹配前面的表達式0次或更多次。 (全部匹配)。

那麼富,運行後更換,將是「TheCatInTheHat」

同這個表達式邏輯可以適用於scanner.next(String regex);

希望這有助於更多一點,我不是最好的解釋在:)

+0

+1好的建議,分成字段 - 改變輸入格式是一個非常簡單的方法來做到這一點。 –

+0

我使用第一個。但我不知道如何使用正則表達式。 –

2

您可以使用StreamTokenizer進行此操作。如果在String上運行,則用StringReader包裝它。如果在一個文件上運行,只需將您的Reader傳遞給它。

// Replace 「 and 」 with " to make parsing easier; do this only if you truly are 
// using pretty quotes (as you are in your post). 
inputString = inputString.replaceAll("[「」]", "\""); 

StreamTokenizer tokenizer = new StreamTokenizer(new StringReader(inputString)); 
tokenizer.resetSyntax(); 
tokenizer.whitespaceChars(0, 32); 
tokenizer.wordChars(33, 255); 
tokenizer.quoteChar('\"'); 

while (tokenizer.nextToken() != StreamTokenizer.TT_EOF) { 
    // tokenizer.sval will contain the token 
    System.out.println(tokenizer.sval); 
} 

您必須對非ASCII文本使用適當的配置,以上只是一個示例。

如果您想單獨提取數字,則默認StreamTokenizer配置正常,但它使用double並且不提供int數字標記。令人煩惱的是,不可能簡單地禁用數字解析而不重置從頭開始的語法。

如果您不想混淆所有這些,也可以考慮將輸入格式更改爲更方便的內容,如Steve Sarcinella's good suggestion(如果適用)。

相關問題