Tag: 正则表达式

@Path和正则表达式(Jersey / REST)

我在REST项目中使用Jersey ,我需要使用正则表达式 。 挖掘它很简单,如: @Path(“/resources”) public class MyResource { @GET @Path(“{subResources:.*}”) public String get(@PathParam(“subResources”) String subResources) {…} } 但是,这样做, 只有当我传递1个参数时,该方法才会获取请求,例如: GET:… / resources / firstSubResource 如果我使用多于1个参数,则该方法无法获取请求,例如: GET:… / resources / firstSubResource / seccondSubResource / thirdSubResource 如果我的@Path包含变量或文本值 ,我只能使用正则表达式,例如: @Path(“{SubResource1}/{subResources:.*}”) 要么 @Path(“/hardCodeString/{subResources:.*}”) 今天我可以使用这个变量的解决方案来运行,但对于我的观点来说并不是oK。 我的web.xml (…) Jersey Spring Web Application com.sun.jersey.spi.spring.container.servlet.SpringServlet com.sun.jersey.config.property.packages com.myproject.rest 1 Jersey Spring Web Application […]

在正则表达式中设置最小和最大字符?

我写了一个正则表达式 ,它匹配任意数量的字母,字母之间有任意数量的单个空格。 我希望这个正则表达式也强制执行最小和最大数量的字符,但我不知道该怎么做(或者如果可能的话)。 我的正则表达式是: [A-Za-z](\s?[A-Za-z])+ 我意识到它只是围绕一个空格匹配两组字母,所以我稍微修改它来修复它。 最初的问题仍然是相同的。 有没有办法强制执行至少三个字符,最多30个字符?

Java string.split – 由多个字符分隔符

我想根据所有可能的分隔符解析整个文件,如逗号,冒号,冒号,句号,空格,hipns等。 假设我有一个假设的字符串行“你好,X怎么样:你呢?” 我应该得到输出数组的项目Hi,X,How,how,are,any和you。 如何在String.split方法中指定所有这些分隔符? 提前致谢。

java分裂函数

有人可以帮助我理解java中的split是如何工作的。我有以下代码 String temp_array[]; String rates = “RF\\0.6530\\0.6535\\D”; String temp = rates.substring(1, rates.length()); System.out.println(temp);// prints F\0.6530\0.6535\D String regex = “\\”; temp_array = temp.split(regex); String insertString = “INSERT into table values(“+temp_array[0]+”,”+temp_array[1]+”,”+temp_array[2]+”,”+temp_array[3]+”)”; 但是在split函数中我得到以下exception Exception in thread “main” java.util.regex.PatternSyntaxException: Unexpected internal error near index 1 \ ^ at java.util.regex.Pattern.error(Unknown Source) at java.util.regex.Pattern.compile(Unknown Source) at java.util.regex.Pattern.(Unknown Source) at java.util.regex.Pattern.compile(Unknown Source) […]

一次替换多个子串

假设我有一个文件,其中包含一些文字。 其中有子字符串,如“substr1”,“substr2”,“substr3”等。 我需要用其他一些文本替换所有这些子串,例如“repl1”,“repl2”,“repl3”。 在Python中,我会创建一个这样的字典: { “substr1”: “repl1”, “substr2”: “repl2”, “substr3”: “repl3” } 并创建用’|’连接键的模式,然后用re.sub函数替换。 在Java中是否有类似的简单方法?

Java Regex替换为捕获组

可能重复: Java Regex替换为捕获组 有没有办法用修改后的捕获组内容替换正则表达式? 例: Pattern regex = Pattern.compile(“(\\d{1,2})”); Matcher regexMatcher = regex.matcher(text); resultString = regexMatcher.replaceAll(“$1”); // *3 ?? 我想用$ 1替换所有出现次数乘以3。 编辑: 看起来,有些不对劲:( 如果我使用 Pattern regex = Pattern.compile(“(\\d{1,2})”); Matcher regexMatcher = regex.matcher(“12 54 1 65”); try { String resultString = regexMatcher.replaceAll(regexMatcher.group(1)); } catch (Exception e) { e.printStackTrace(); } 它抛出IllegalStateException:找不到匹配项 但 Pattern regex = Pattern.compile(“(\\d{1,2})”); Matcher […]

花花括号之间的Java正则表达式匹配

我需要解析一个日志文件并获取时间和相关的函数调用字符串。它存储在日志文件中,如下所示:{“time”:“2012-09-24T03:08:50”,“message”:“Call( )开始“} 其他字符串字符之间将有多个记录时间函数调用,因此我希望使用正则表达式来遍历文件并获取所有这些 我想获取包括大括号在内的整个记录​​信息 我尝试了以下内容 Pattern logEntry = Pattern.compile(“{(.*?)}”); Matcher matchPattern = logEntry.matcher(file); 和 Pattern.compile(“{[^{}]*}”); Matcher matchPattern = logEntry.matcher(file); 我一直得到非法重复错误,请帮忙! 谢谢。

通过java中的标点符号和空格等通过正则表达式拆分字符串

我有这个文本文件,我读入Java应用程序,然后逐行计算其中的单词。 现在我正在将这些行分成一个单词 String.split([\\p{Punct}\\s+])” 但我知道我错过了文本文件中的一些单词。 例如,“不能”这个词应该分为两个词“can”和“t”。 应完全忽略逗号和其他标点符号,并将其视为空格。 我一直试图理解如何形成一个更精确的正则表达式来做到这一点,但我是一个新手,所以我需要一些帮助。 对于我所描述的目的,什么可以是更好的正则表达式?

Java Regexp匹配ASCII字符

什么正则表达式匹配java中的任何ASCII字符? 我已经尝试过了: ^[\\p{ASCII}]*$ 但发现它与我想要的很多东西(如空格,括号等等)不匹配。 我希望避免以下列格式明确列出所有127个ASCII字符: ^[a-zA-Z0-9!@#$%^*(),.~`[]{}\\/+=-\\s]*$

Java:基于正则表达式在HashMap密钥中搜索?

我正在使用HashMap构建同义词库以存储同义词。 我正在尝试基于正则表达式搜索单词:该方法必须将字符串作为参数并返回结果数组。 这是我第一次尝试: public ArrayList searchDefinition(String regex) { ArrayList results = new ArrayList(); Pattern p = Pattern.compile(regex); Set keys = thesaurus.keySet(); Iterator ite = keys.iterator(); while (ite.hasNext()) { String candidate = ite.next(); Matcher m = p.matcher(candidate); System.out.println(“Attempting to match: ” + candidate + ” to ” + regex); if (m.matches()) { System.out.println(“it matches”); results.add(candidate); } […]