如何使正则表达式匹配测量单位?
我正在构建一个小型 Java 库,它必须匹配字符串中的单位。例如,如果我有“300000000 m/s^2”,我希望它与“m”和“s^2”匹配。
到目前为止,我已经尝试了最能想象到的(我自己)类似的配置(我希望这是一个好的开始)
"[[a-zA-Z]+[\\^[\\-]?[0-9]+]?]+"
为了澄清,我需要一些与 letters[^[-]numbers]
匹配的东西(其中 [ ]表示非强制性部分)。这意味着:字母,后面可能跟着一个可能为负数的指数。
我研究了一点正则表达式,但我真的不太流利,所以任何帮助将不胜感激!
非常感谢,
编辑: 我刚刚尝试了前 3 个回复
String regex1 = "([a-zA-Z]+)(?:\\^(-?\\d+))?";
String regex2 = "[a-zA-Z]+(\\^-?[0-9]+)?";
String regex3 = "[a-zA-Z]+(?:\\^-?[0-9]+)?";
,但它不起作用...我知道测试模式的代码有效,因为如果我尝试一些简单的操作,例如在“12345”中匹配“[0-9]+”,它将匹配整个字符串。所以,我不明白还有什么问题。我正在尝试在目前需要的地方更改我的方括号...
用于测试的代码:
public static void main(String[] args) {
String input = "30000 m/s^2";
// String input = "35345";
String regex1 = "([a-zA-Z]+)(?:\\^(-?\\d+))?";
String regex2 = "[a-zA-Z]+(\\^-?[0-9]+)?";
String regex3 = "[a-zA-Z]+(?:\\^-?[0-9]+)?";
String regex10 = "[0-9]+";
String regex = "([a-zA-Z]+)(?:\\^\\-?[0-9]+)?";
Pattern pattern = Pattern.compile(regex3);
Matcher matcher = pattern.matcher(input);
if (matcher.matches()) {
System.out.println("MATCHES");
do {
int start = matcher.start();
int end = matcher.end();
// System.out.println(start + " " + end);
System.out.println(input.substring(start, end));
} while (matcher.find());
}
}
I'm building a small Java library which has to match units in strings. For example, if I have "300000000 m/s^2", I want it to match against "m" and "s^2".
So far, I have tried most imaginable (by me) configurations resembling (I hope it's a good start)
"[[a-zA-Z]+[\\^[\\-]?[0-9]+]?]+"
To clarify, I need something that will match letters[^[-]numbers]
(where [ ] denotes non obligatory parts). That means: letters, possibly followed by an exponent which is possibly negative.
I have studied regex a little bit, but I'm really not fluent, so any help will be greatly appreciated!
Thank you very much,
EDIT:
I have just tried the first 3 replies
String regex1 = "([a-zA-Z]+)(?:\\^(-?\\d+))?";
String regex2 = "[a-zA-Z]+(\\^-?[0-9]+)?";
String regex3 = "[a-zA-Z]+(?:\\^-?[0-9]+)?";
and it doesn't work... I know the code which tests the patterns work, because if I try something simple, like matching "[0-9]+" in "12345", it will match the whole string. So, I don't get what's still wrong. I'm trying with changing my brackets for parenthesis where needed at the moment...
CODE USED TO TEST:
public static void main(String[] args) {
String input = "30000 m/s^2";
// String input = "35345";
String regex1 = "([a-zA-Z]+)(?:\\^(-?\\d+))?";
String regex2 = "[a-zA-Z]+(\\^-?[0-9]+)?";
String regex3 = "[a-zA-Z]+(?:\\^-?[0-9]+)?";
String regex10 = "[0-9]+";
String regex = "([a-zA-Z]+)(?:\\^\\-?[0-9]+)?";
Pattern pattern = Pattern.compile(regex3);
Matcher matcher = pattern.matcher(input);
if (matcher.matches()) {
System.out.println("MATCHES");
do {
int start = matcher.start();
int end = matcher.end();
// System.out.println(start + " " + end);
System.out.println(input.substring(start, end));
} while (matcher.find());
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您匹配单个字符,则无需使用字符类
[
...]
。(
...)
这里是一个捕获括号,供您稍后提取单位和指数。(?:
...)
是非捕获分组。You don't need to use the character class
[
...]
if you're matching a single character.(
...)
here is a capturing bracket for you to extract the unit and exponent later.(?:
...)
is non-capturing grouping.您混合使用方括号来表示字符类和大括号来进行分组。请尝试这样做:
在许多正则表达式方言中,您可以使用 \d 来表示任何数字而不是 [0-9]。
You're mixing the use of square brackets to denote character classes and curly brackets to group. Try this instead:
In many regular expression dialects you can use \d to mean any digit instead of [0-9].
尝试
Try