用于匹配文件中的十六进制数字的 Java 正则表达式
所以我正在读取一个文件(如 java 程序
58
68
58
68
40
c
40
48
FA
如果我幸运的话,但更常见的是,它在每行之前和之后都有几个空白字符。
这些是我正在解析的十六进制地址,我基本上需要确保我可以使用扫描仪、缓冲阅读器等来获取该行,并确保我可以将十六进制转换为整数。这就是我到目前为止所拥有的:
Scanner scanner = new Scanner(System.in);
int address;
String binary;
Pattern pattern = Pattern.compile("^\\s*[0-9A-Fa-f]*\\s*$", Pattern.CASE_INSENSITIVE);
while(scanner.hasNextLine()) {
address = Integer.parseInt(scanner.next(pattern), 16);
binary = Integer.toBinaryString(address);
//Do lots of other stuff here
}
//DO MORE STUFF HERE...
所以我已经将所有错误追溯到解析输入和内容,所以我想我只是想找出需要什么正则表达式或方法才能使其按我想要的方式工作。
So I'm reading in a file (like java program < trace.dat) which looks something like this:
58
68
58
68
40
c
40
48
FA
If I'm lucky but more often it has several whitespace characters before and after each line.
These are hexadecimal addresses that I'm parsing and I basically need to make sure that I can get the line using a scanner, buffered reader... whatever and make sure I can then convert the hexadecimal to an integer. This is what I have so far:
Scanner scanner = new Scanner(System.in);
int address;
String binary;
Pattern pattern = Pattern.compile("^\\s*[0-9A-Fa-f]*\\s*$", Pattern.CASE_INSENSITIVE);
while(scanner.hasNextLine()) {
address = Integer.parseInt(scanner.next(pattern), 16);
binary = Integer.toBinaryString(address);
//Do lots of other stuff here
}
//DO MORE STUFF HERE...
So I've traced all my errors to parsing input and stuff so I guess I'm just trying to figure out what regex or approach I need to get this working the way I want.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
s.next()
负责处理空格。 (默认的分词器不关心它们。)如果您真的想坚持使用模式方法,我建议您使用 XDigit 类
:
scanner.next(pattern)
将返回整个匹配的模式(包括空格!)您需要使用捕获组。尝试该模式然后使用 matcher.group(1)
The
s.next()
takes care of the white-spaces. (The default tokenizer doesn't care about them.)If you'd really like to stick with the Pattern-approach, I would recommend you to use the XDigit class:
Further more; The
scanner.next(pattern)
will return the entire matched pattern (including the white-spaces!) You need to work with capturing groups. Try the patternAnd then get the actual hex-number with matcher.group(1)