简体   繁体   English

从Java中的字符串中提取子字符串

[英]Extracting substrings from a string in Java

I have a number of files and they are all called something like name_version_xyz.ext . 我有很多文件,它们都被称为name_version_xyz.ext东西。

In my Java code I need to extract the name and the version part of the filename. 在我的Java代码中,我需要提取文件名的名称和版本部分。 I can accomplish this using lastIndexOf where I look for underscore, but I don't think that's the nicest solution. 我可以在寻找下划线的地方使用lastIndexOf完成此操作,但我认为这不是最好的解决方案。 Can this be done with a regexp somehow? 可以用正则表达式来完成吗?

Note that the "name" part can contain any number of underscores. 请注意,“名称”部分可以包含任意数量的下划线。

If you are guaranteed to having the last part of your files named _xyz.ext, then this is really the cleanest way to do it. 如果可以保证文件的最后一部分名为_xyz.ext,那么这实际上是最干净的方法。 (If you aren't guaranteed this, then, you will need to figure out something else, of course.) (如果不能保证这一点,那么您当然需要弄清楚其他事情。)

As the saying goes with regular expressions: 俗话说正则表达式:

If you solve you a problem with regular expressions, you now have two problems. 如果使用正则表达式解决问题,那么现在有两个问题。

You could use Regex but I think it is a bit overkill in this case. 您可以使用Regex,但在这种情况下,我认为这有点过头了。 So I personally would stick with your current solution. 所以我个人会坚持使用您当前的解决方案。

It is working, not too complicated and that's why I don't see any reasons to switch to another approach. 它是有效的,并不太复杂,这就是为什么我看不出有任何理由切换到另一种方法的原因。

If you don't want to use regular expression I think the easiest solution is when you retrieve files and get only part without extension and then: 如果您不想使用正则表达式,我认为最简单的解决方案是在检索文件时仅获取不带扩展名的部分,然后执行以下操作:

String file = "blah_blah_version_123";
String[] tmp = file.split("_version_");
System.out.println("name = " + tmp[0]);
System.out.println("version = " + tmp[1]);

Output: 输出:

name = blah_blah
version = 123

Yes, the regexp as a Java string would just look something like (untested) 是的,正则表达式作为Java字符串看起来就像(未经测试)

(.+)_(\\d+)_([^_]+)\\.(???)

"name" would be group(1), "version" group(2), xyz is group(3), and ext is group(4). “名称”为group(1),“版本”为group(2),xyz为group(3),而ext为group(4)。

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

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