Find years with regex

Darling, I would remove the lines from the list containing the date. Example: "Musical groups of the 1990s" should be deleted.

Can I do this in java?

+3
source share
4 answers

If you use a date format YYYY, the regex ^\d{4}should work for any date at the beginning of a line.

String str = "1990s music groups";
boolean shouldDelete = str.matches("^\d{4}");
if (shouldDelete) {
    // Delete string
}

If you want to combine the date in any part of the string, just delete the leading ^.

+1
source

Regex \d{4}is likely to be sufficient - it will match all lines containing 4 subsequent digits. Perhaps you may have more specific cases, for example19\d{2}|20\d{2}

+2
source
//ArrayList<String> list = ....
String year = "1990";
ArrayList<String> toRemove = new ArrayList<String>();
for (String str:list) {
   if (str.matches(".*"+year+".*")) {
      toRemove.add(str);
   }
}
for (String str:toRemove) list.remove(str);
toRemove = null;

toRemove, java.util.ConcurrentModificationException

0

(1970-2029) :

Pattern pattern;
Matcher matcher;
String errorTag = null;
private static final String PATTERN_YEAR = "^(197\\d{1}|198\\d{1}|199\\d{1}|200\\d{1}|201\\d{1}|202\\d{1})";

...

if (filter.getName().contains("YYYY")){
    pattern = Pattern.compile(PATTERN_YEAR);
    matcher = pattern.matcher(filter.getValue());
      if(!matcher.matches()){   
          errorTag= " *** The year is invalid, review the rate";


    }
}
0
source

Source: https://habr.com/ru/post/1796528/


All Articles