简体   繁体   English

删除字符串中的特定单词

[英]Remove specific words in a string

Given a string = "test20190906.pdf", how can I get only "test.pdf" such that it removes the date from the string by using string.replace or remove ? 给定一个string =“test20190906.pdf”,如何才能获得“test.pdf”,以便通过使用string.replace或remove来删除字符串中的日期?

Considering the format will always be filename + date + .extension. 考虑到格式将始终是文件名+日期+ .extension。

您可以使用正则表达式删除类似于任何格式的日期的 连续数字,前提是文件名紧跟日期附加。

"test20190906.pdf".replaceAll("[0-9]{8}\\.", "."));

I see previous answers and that answers does not work if you got other numbers in file name for example: 01_test20190913.pdf 我看到以前的答案,如果您在文件名中有其他数字,答案不起作用,例如: 01_test20190913.pdf

In that case solution will be 在那种情况下解决方案将是

String file = "01_test20190913.pdf";
System.out.println(file.substring(0, file.length() - 12)+".pdf");

here i take the first part of string without last 12 characters and add ".pdf" 这里我把字符串的第一部分没有最后12个字符并添加“.pdf”

There are a lot of good answers, but I want present one more. 有很多好的答案,但我想再提出一个。 It'll work if filename contains digits not only in date part. 如果文件名不仅包含日期部分中的数字,它也会起作用。 I assume that date is always appears before extension and has fixed length. 我假设日期始终在扩展之前出现并且具有固定长度。

s.replaceAll("\\d{8}\\.pdf", ".pdf");

And if the file extension varies then you could do some additional work: 如果文件扩展名不同,那么您可以做一些额外的工作:

public static String removeDate(String s) {
    final String extension = s.substring(s.lastIndexOf("."));
    final String pattern = "\\d{8}\\" + extension;

    return s.replaceAll(pattern, extension);
}

public static void main(String args[])
{
    System.out.println(removeDate("test20190101.pdf"));
    System.out.println(removeDate("123123test20190101.txt"));
    System.out.println(removeDate("123te11st20190101.csv"));
}

This can be done with the regexp only, but at the cost of readability. 这可以仅使用正则表达式完成,但代价是可读性。

Assuming the date contains only numbers, you can use regex to replace numbers, eg: 假设日期仅包含数字,您可以使用regex替换数字,例如:

String fileNameWithDate = "test20190906.pdf";
String fileName = fileNameWithDate.replaceAll("[0-9]+", ""));
System.out.println(fileName);

If the format of date is "yyyyMMdd" then I suggest go for the simplest solution as also given by @pavelbere. 如果日期的格式是“yyyyMMdd”,那么我建议选择@pavelbere给出的最简单的解决方案。 But this solution also assumes that the date always appends in the end of the filename. 但是这个解决方案还假设日期总是附加在文件名的末尾。

String file = "test20190906.pdf"; 
String fileName = file.substring(0, file.length() - 12)+".pdf";
string name = "test20190906.pdf"
name.replaceAll("[0-9]","");

My approach would be to remove all numbers which are 8 digits long and are next to the last dot and replace them with a dot using the regex: (\\d{8})(?!.*\\d\\.)\\. 我的方法是删除所有8位数的数字并且在最后一个点旁边,并使用正则表达式替换它们: (\\d{8})(?!.*\\d\\.)\\.

String filename = "filename12345678.pdf";
filename = filename.replaceAll("(\\d{8})(?!.*\\d\\.)\\.", ".");

You can see this being used and an explanation of what it does here . 你可以看到这个被使用,并解释它在这里做了什么。

If the date can be different lengths then replace the {8} with a * , this enables the date to be any length. 如果日期可以是不同的长度,则将{8}替换为* ,这样可以使日期为任意长度。

An answer that doesn't use Regex: For filename as the original string: 不使用正则表达式的答案:将filename作为原始字符串:

l = filename.split('.')
l[-2] = l[-2][:-8]
output = '.'.join(l)

This uses the fact that the last '.' 这使用了最后一个'。'的事实。 will always precede the extension, so the 8 characters prior to this will be dates. 将始终在扩展名之前,因此在此之前的8个字符将是日期。 As long as we remove those, and put the '.' 只要我们删除那些,并把'。' back in, we have the filename regardless of extension, regardless of characters preceding it, without using Regular Expressions. 回来,我们有文件名,无论扩展名如何,无论前面的字符如何,都不使用正则表达式。

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

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