- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想知道正则表达式匹配失败时错误的确切位置。但我在 Matcher 和 Pattern 中都找不到任何可以做到这一点的类或方法。有什么帮助吗?
Pattern regExpPattern = Pattern.compile("My regular expression");
Matcher matcher = regExpPattern.matcher("Input string");
if (!matcher.matches()) {
// Better error handling here
throw new Exception("Invalid expression");
}
编辑:以下是工作代码摘录:
public class FOPRequestParser {
private static Pattern regExpPattern;
private static String requestRegexp;
private static String regexpSeparators;
private static String[] INPUT_FORMATS = new String[] {"FO", "IFP"};
private static String[] OUTPUT_FORMATS = new String[] {"PDF", "PS", "AFP", "IFP"};
static {
regexpSeparators = new String("(\\Q|@|\\E|=)");
requestRegexp = new String("run" + regexpSeparators + "[a-zA-Z]+.*"
+ regexpSeparators + "inputFormat=(");
for (int i = 0; i < INPUT_FORMATS.length; ++i) {
requestRegexp += INPUT_FORMATS[i];
if (i != INPUT_FORMATS.length - 1) {
requestRegexp += "|";
}
}
requestRegexp += ")" + regexpSeparators + "[a-zA-Z]+.*"
+ regexpSeparators + "outputFormat=(";
for (int i = 0; i < OUTPUT_FORMATS.length; ++i) {
requestRegexp += OUTPUT_FORMATS[i];
if (i != OUTPUT_FORMATS.length - 1) {
requestRegexp += "|";
}
}
requestRegexp += ")";
}
public FOPRequestParser() {
regExpPattern = Pattern.compile(requestRegexp);
}
public void processRequest(String request) throws Exception {
Matcher matcher = regExpPattern.matcher(request);
if (!matcher.matches()) {
throw new Exception("Invalid expression");
}
}
public static void main(String[] args) {
FOPRequestParser conversion = new FOPRequestParser();
try {
String exp1 = new String("run|@|" +
"c:\\input_file.fo|@|" +
"inputFormat=FO|@|" +
"c:\\output_file.pdf|@|" +
"outputFormat=PDF");
conversion.processRequest(exp1);
System.out.println("Valid expression");
} catch (Exception e) {
e.printStackTrace();
}
}
}
最佳答案
没有 API 方法可以告诉您为什么不匹配。
如果您将其用于任何类型的(输入)验证,您必须抛出一个通用异常,例如“提供的值与预期模式不匹配
关于Java正则表达式: finding match errors,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20786288/
我有一个 合作伙伴集合,我正在使用 pymongo 来检索数据 当我使用 MongoDB 查询集合时,我看到以下结果 db.partner.find({'unique_key': 'c89dbe313
嗨,我正在尝试在一个 find 命令中查找所有 js 和 css 文件。我尝试了以下所有方法但徒劳无功: find WebContent -name "*.[jc]ss?" find WebConte
我使用以下 find 命令查找并显示所有具有输入文本模式的文件。 找 。 -type f -print|xargs grep -n "模式" 我有很多项目文件夹,每个文件夹都有自己的名为“Makefi
我在Windows环境中使用Gnuwin32二进制文件。 当我想查找某种类型的文件时(例如PDF),我通常运行: find . -iname '*.pdf' -print 这在任何UNIX系统上均可完
我使用的是 Julia 编程语言,我知道你可以通过以下方式使用 find 函数: a = [ 1 2 3 4 3 5 3 6 7 8 9 3 ] find(a .== 3) 它将返回:3,5,7,12
jsperf's link 我不是 jQuery 专家(甚至不是一个好的用户),我没有研究它的整个源代码(只有一小部分不能帮助我解决这个问题)。 有人可以为我解释一下吗? 最佳答案 这个: $p.fi
我应该如何在 CentOS 7 中修复这个错误? [jalal@goku HW4]$ git clone https://github.com/pathak22/pyflow.git Cloning
是否可以更改传递给 find 中的 exec 的参数?例如,我需要以不同的名称复制文件:*.txt -> *.new.txt现在我正在为两个命令执行此操作: find /root/test -name
我想通过cleartool find 命令找到*.cs 和*.cpp 文件。但它失败了。 cleartool find "M:\test_view\code" -name "*.cs *.cpp"
我正在使用 PyMongo,看到有人建议使用 find()[:] 而不是 find()。很好奇有什么区别? 最佳答案 [:] 制作列表的浅拷贝,因此对对象的引用是相同的。我查看了 Pymongo 文档
我正在处理文件和目录,以在每个目录中查找最近修改的文件。我的代码可以工作,但作为 Ruby 的新手,我无法正确处理错误。 我使用 Find.find 获取递归目录列表,为每个目录调用我自己的函数 ne
/usr/bin/ld: cannot find -ldlib /usr/bin/ld: cannot find -lcblas /usr/bin/ld: cannot find -llapack 在
我有一些数据文件的一系列索引文件,它们基本上采用这种格式 索引文件:asdfg.log.1234.2345.index 数据文件:asdfg.log 这个想法是搜索所有索引文件。如果值 XXXX 出现
我有一个 find我运行以查找名称包含 foo 的文件的命令. 我想跳过 .git目录。下面的命令有效 除了 它打印一个 烦人 .git任何时候它跳过 .git目录: find . ( -name .
我有以下想做的事情: find . -maxdepth 6 \( -name \*.tar.gz -o -name bediskmodel -o -name src -o -name ciao -o
当我在表中查找隐藏字段时,我看到了两个隐藏字段。但是,我想通过 ID 进一步细化这两个字段。我注意到,当我使用“包含”在整个表上使用 find 时,我得到了 2 个字段。但是,如果我对隐藏字段的查找结
我正在使用下面的命令生成文件列表及其 m5sum。问题是某些文件或文件夹的名称中有空格。我将如何处理这些? find -type f -name \* | xargs md5sum 最佳答案 尝试:
我正在使用下面的命令生成文件列表及其 m5sum。问题是某些文件或文件夹的名称中有空格。我将如何处理这些? find -type f -name \* | xargs md5sum 最佳答案 尝试:
我有一个使用正则表达式查找文件的脚本。代码如下: find $dir | grep "$regex" 脚本运行有点慢,我想优化一下。搜索需要一些时间来执行,我想从中获得更好的性能。我试过这种尝试: f
这令人沮丧。我认为问题出在 api 响应返回的对象上。也许它是在字符串中,所以我所做的就是复制“postman”的响应并将其直接粘贴到js上。这样我就可以确定它在对象/数组中。但结果还是同样的错误。
我是一名优秀的程序员,十分优秀!