Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/345.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使用spring捕获与模式匹配的组,并将匹配的模式连接到另一个字符串?_Java_Regex_Spring - Fatal编程技术网

Java 如何使用spring捕获与模式匹配的组,并将匹配的模式连接到另一个字符串?

Java 如何使用spring捕获与模式匹配的组,并将匹配的模式连接到另一个字符串?,java,regex,spring,Java,Regex,Spring,可以有像sshexclude1 sshexclude2这样的字符串,我只想捕获“1”和“2”,即sshexclude后面的数字,并将其保存为ssh*,如何提取该值。这就是我到目前为止所尝试的。。(我想将连接的值保存在string类型的bean中) 我不认为这与Spring有任何关系,但您可以只做一个简单的字符串正则表达式替换,例如: String result = "sshexclude1".replaceFirst("exclude", ""); 将为您提供“ssh1”用于捕获正则表达式中字

可以有像sshexclude1 sshexclude2这样的字符串,我只想捕获“1”和“2”,即sshexclude后面的数字,并将其保存为ssh*,如何提取该值。这就是我到目前为止所尝试的。。(我想将连接的值保存在string类型的bean中)


我不认为这与Spring有任何关系,但您可以只做一个简单的字符串正则表达式替换,例如:

String result = "sshexclude1".replaceFirst("exclude", "");
将为您提供“ssh1”

用于捕获正则表达式中
字符串的部分。它们的应用程序在应用程序上下文文件中与在Java代码中没有区别:

<bean id="sshExcludeValue" class="java.util.regex.Pattern" factory-method="compile">
   <constructor-arg value="sshexclude(\d+)" />
</bean>
然后使用
Matcher\matches
提取数字

请注意,当与完整的
字符串匹配时,会删除线锚的起点和终点

<bean id="sshExcludeValue" class="java.util.regex.Pattern" factory-method="compile">
   <constructor-arg value="sshexclude(\d+)" />
</bean>
@Autowired
Pattern pattern;
String str = "sshexclude2";
Matcher matcher = pattern.matcher(str);
if (matcher.matches()) {
   String newString = "ssh" + matcher.group(1);
}