如何拆分读取线,但不要在撇号中拆分值?
示例TXT文件
ADD 'Cordless Screwdriver' 30 1 2
COST 'Multi Bit Ratcheting'
FIND 'Thermostat'
FIND 'Hand Truck'
SELL 'Hammer' 1
QUANTITY 'Paint Can'
FIRE 'Joshua Filler'
HIRE 'Lewis hamilton' 35 G
PROMOTE 'Lewis hamilton' M
SCHEDULE
代码
File inputFile = new File("src/edu/iu/c212/resources/input.txt");
String[] inputWords = null;
FileReader inputReader = new FileReader(inputFile);
BufferedReader bri = new BufferedReader(inputReader);
String y;
while ((y = bri.readLine()) != null) {
inputWords = y.split(" ");
--- Project Code That Handles Split Up Lines ---
}
有没有办法在跨行线时可以在撇号中使用拆分拆分项目?这样,无论第一项是一个或两个单词,如果我调用输入字[1],它将始终返回完整的字符串。
发生了什么:“多重棘轮” - >输入字[1] - > 'Multi
我想要的:“ Multi bit ratcheting” - >输入字[1] - > “多重棘轮”
Example txt file
ADD 'Cordless Screwdriver' 30 1 2
COST 'Multi Bit Ratcheting'
FIND 'Thermostat'
FIND 'Hand Truck'
SELL 'Hammer' 1
QUANTITY 'Paint Can'
FIRE 'Joshua Filler'
HIRE 'Lewis hamilton' 35 G
PROMOTE 'Lewis hamilton' M
SCHEDULE
Code
File inputFile = new File("src/edu/iu/c212/resources/input.txt");
String[] inputWords = null;
FileReader inputReader = new FileReader(inputFile);
BufferedReader bri = new BufferedReader(inputReader);
String y;
while ((y = bri.readLine()) != null) {
inputWords = y.split(" ");
--- Project Code That Handles Split Up Lines ---
}
Is there a way I can have the split not split items within the apostrophes when going across the line? That way regardless if the first Item is one word or two words, if I call inputWords[1] It will always return the full string.
What happens: "Multi Bit Ratcheting" -> inputWords[1] -> 'Multi
What I want: "Multi Bit Ratcheting" -> inputWords[1] -> 'Multi Bit Ratcheting'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用模式
'。*?'| \ s+
:您可以将上述逻辑应用于文件中的每一行。但是,您应该定义循环外部图案,以便不必为每行重新编译。
您更新的代码:
You could apply a regex find all to each line using the pattern
'.*?'|\S+
:You may apply the above logic to each line from the file. You should, however, define the pattern outside the loop so that it doesn't have to be recompiled for every line.
Your updated code: