如果你想在失败时立即中止,不如搜索错误的输入呢?以下搜索不符合您的条件的条目(超过三个字符,非 x/y/z,null):
"(|[\w\s]{4,}|[\w]{0,2}[^xyz][\w]{0,2})"
捕获错误输入的一些示例:
$ grep -P '"(|[\w\s]{4,}|[\w]{0,2}[^xyz][\w]{0,2})"' user_input.txt
"xya", "yyy", "zzz", 'x', new ItemStack(Item.stick, 1, 0) , 'y', new ItemStack(Item.stick, 1, 0) , 'z', new ItemStack(Block.stone, 1, 0)
"xxxx", "yyy", "zzz", 'x', new ItemStack(Item.stick, 1, 0) , 'y', new ItemStack(Item.stick, 1, 0) , 'z', new ItemStack(Block.stone, 1, 0)
"xxx", "yag", "zzz", 'x', new ItemStack(Item.stick, 1, 0) , 'y', new ItemStack(Item.stick, 1, 0) , 'z', new ItemStack(Block.stone, 1, 0)
"xxx", "yyy", "zzo", 'x', new ItemStack(Item.stick, 1, 0) , 'y', new ItemStack(Item.stick, 1, 0) , 'z', new ItemStack(Block.stone, 1, 0)
"(", "yxy", "yzz", 'x', new ItemStack(Item.stick, 1, 0) , 'y', new ItemStack(Item.stick, 1, 0) , 'z', new ItemStack(Block.stone, 1, 0)
"", "yxy", "yzz", 'x', new ItemStack(Item.stick, 1, 0) , 'y', new ItemStack(Item.stick, 1, 0) , 'z', new ItemStack(Block.stone, 1, 0)
查看不匹配的示例(即良好的输入):
$ grep -vP '"(|[\w\s]{4,}|[\w]{0,2}[^xyz][\w]{0,2})"' user_input.txt
"xxx", "yyy", "zzz", 'x', new ItemStack(Item.stick, 1, 0) , 'y', new ItemStack(Item.stick, 1, 0) , 'z', new ItemStack(Block.stone, 1, 0)
"x", "yxy", "yzz", 'x', new ItemStack(Item.stick, 1, 0) , 'y', new ItemStack(Item.stick, 1, 0) , 'z', new ItemStack(Block.stone, 1, 0)