简体   繁体   English

正则表达式查找第一个下划线之前和之后的所有字符,然后确保它们相同

[英]Regex to find all characters before and after first underscore, then make sure they are the same

I have these two strings 我有这两个弦

2014_UMW
2014_UMW_web

I need to write a regex to get the character before and after the fist underscore. 我需要写一个正则表达式来获得拳头下划线前后的字符。 Then I need to make sure that they are both the same. 然后,我需要确保它们都相同。 I am checking to make sure that 2014_UMW is at the beginning of both strings. 我正在检查以确保2014_UMW在两个字符串的开头。 2014_UMW is only one example. 2014_UMW只是一个示例。 It could be 2015_YYY and 2015_YYY_web etc. 可能是2015_YYY2015_YYY_web等。

This the is regex that I am using (?<=_)[^_]+(?=_) and then I am using pattern and matcher methods to see if they are both the same, but it is not working right. 我正在使用(?<=_)[^_]+(?=_)正则表达式,然后使用模式和匹配器方法来查看它们是否相同,但无法正常工作。 I have also tried this regex [a-zA-Z_0-9]+[^_]+(?=_) . 我也尝试过此正则表达式[a-zA-Z_0-9]+[^_]+(?=_)

To get the part after first _ you can use this regex: 要先获得_之后的零件,可以使用此正则表达式:

Pattern p = Pattern.compile("^[^_]+_([^_]+)");

and get the captured group #1 using matcher.group(1) for the part your're interested in. 然后使用matcher.group(1)获取您感兴趣的部分的捕获的#1组。

RegEx Demo 正则演示

The pattern you say you are using, (?<=_)[^_]+(?=_) , matches a non-empty sequence of characters other than '_' that is bounded on each side by an underscore. 您说的正在使用的模式(?<=_)[^_]+(?=_)匹配一个非空字符序列,除了'_' ,每个字符都由下划线界定。 That's nothing like your intent to "get the character before and after the [first] underscore". 这与您要“在[第一个]下划线之前和之后获取角色”的意图完全不同。

From your example, I think what you mean to do is split the strings at underscores, and compare the first two segments of each. 从您的示例中,我认为您的意思是在下划线处分割字符串,然后比较每个字符串的前两个部分。 In that case, you might consider using String.split() . 在这种情况下,您可以考虑使用String.split() Details could vary depending on exactly how you want to characterize the splitting, but here's one, simple, way it might go: 详细信息可能会有所不同,具体取决于您要如何表征分割,但是这是一种可能的简单方法:

String[] parts1 = string1.split("_");
String[] parts2 = string2.split("_");
// compare elements of parts1 and parts2

Alternatively, if you want to use a regex to capture the first two segments of such a string, then you want a Pattern along these lines: 另外,如果您想使用正则表达式来捕获此类字符串的前两个段,那么您需要沿着这些行使用Pattern

Pattern p = Pattern.compile("^([^_]+)_([^_]+)(?:_.*)?");

(That form is suitable for use with any of Matcher.matches() , Matcher.find() , or Matcher.lookingAt() ; simpler forms are possible if you only want to support one or both of the latter two.) Again, details of the needed pattern may vary depending on exactly what you're after. (该形式适合与Matcher.matches()Matcher.find()Matcher.lookingAt() ;如果您只想支持后两者之一或两者,则可以使用更简单的形式。)同样,所需模式的详细信息可能会有所不同,具体取决于您要执行的操作。

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

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