简体   繁体   English

使用Regex设计捕获十六进制数或整数

[英]Capturing a Hexadecimal number or an Integer using Regex design

I am working on a project that is reading a file line by line. 我正在一个正在逐行读取文件的项目中。 In these lines there may or not be a two digit hexadecimal number. 在这些行中,可能有一个两位的十六进制数字。 For example EC, 1F, A3, 34. The line can include words, single digit numbers, a letter, or various other special characters, all are separated by spaces. 例如EC,1F,A3、34。该行可以包括单词,一位数字,字母或各种其他特殊字符,所有这些字符均由空格分隔。 This number will always be in the same spot in the line if it is present. 如果存在此号码,它将始终位于该行的同一位置。 If it is present I will need to do something special to the data on that line than say if a single digit number was present. 如果存在,那么我需要对该行的数据做一些特殊的事情,而不是说是否存在一个数字。 I'm taking the line and placing it into an array as it is used for something else in the program. 我将这行代码放入程序中用于其他用途的数组中。 In regards to the design of capturing this number I am planning on using a regex expression and matches method to grab it from the original string. 关于捕获此数字的设计,我计划使用正则表达式和match方法从原始字符串中获取它。 This will be stored in a data structure somewhere else as I will also have to add or subtract with this later on. 这将存储在其他地方的数据结构中,因为稍后我也将对此进行加减。

Would the most efficient way to capture this method be using a matcher.find() method? 捕获此方法的最有效方法是使用matcher.find()方法吗? Or should I just grab it straight off the line since I know exactly where it will be placed? 还是因为我确切知道它的放置位置,我应该直接将其脱机? I wanted to include matches just in case the user made an error and was off by one in inputting the hexadecimal number and to check if the number is present. 我想包括匹配项,以防万一用户输入十六进制数字时出错并被一个数字取一个并检查数字是否存在。 There is a little room to give in this input on both sides. 双方都可以提供一些输入空间。 I know I could just grab the array and use the index value of where the hexadecimal number is located but I am curious is it better program design to plan for small discrepancies like being off by a space and having that already built into your program or keep it is simple and rigid? 我知道我可以抓取数组并使用十六进制数所在的索引值,但我很好奇, 更好的程序设计是为了计划一些小的差异,例如被空格隔开并已将其内置到您的程序中或保留它是简单而僵化的吗?

This is how I am currently grabbing the hexadecimal number 这是我目前正在获取十六进制数的方式

wordSansNumber = wordToInsert.substring(0,5);
hexaDecimalNumber = wordToInsert.substring(5,14);
justHexaDecimalFinder = hexaDecimalFinder.matcher(hexaDecimalNumber);

If the the match returns true it runs through a few operations. 如果匹配返回true,它将执行一些操作。

I do not think it is a good idea to grab an input from user based on positions. 我认为基于位置从用户那里获取输入不是一个好主意。 User might accidentially add spaces, for instance. 例如,用户可能会意外添加空格。

I think it would be better to trim the line and then, if a number may be only at the start of the line, locate the first space in the line and extract your hex between start of the line and the first space. 我认为最好修整行,然后,如果数字可能仅在行的开头,请找到行中的第一个空格,并在行的开头和第一个空格之间提取十六进制。 Then you must validate it of course. 然后,您当然必须对其进行验证。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM