There is a task: a file comes in, its name contains information, it needs to be pulled out and scattered over the table in the database. Advise what methods for this can be used?

Example file name:

dokument_Article 100938_06072014 beginning = 10.00, con = 11.00 342554658.txt

  • 2
    file.getName () and then parse the resulting string - Andrew Bystrov
  • 2
    Depends on your needs. If the file name has spaces, try Split . Otherwise, use the regulars. Without an example, we cannot know what you need. - Denis
  • @Denis example added to the question description - Varg Sieg
  • You are close! And now we do not know what data you need to get. - Denis
  • 3
    Take the java.util.regex package, write the regulars pattern and find a match via Matcher .......... docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html .. .... as an example String ISBN = "ISBN: 123-456-789-112-3, ISBN: 1234567891123"; Pattern pattern = Pattern.compile("(\\d-?){13}"); Matcher matcher = pattern.matcher(ISBN); while (matcher.find()) System.out.println(matcher.group()); String ISBN = "ISBN: 123-456-789-112-3, ISBN: 1234567891123"; Pattern pattern = Pattern.compile("(\\d-?){13}"); Matcher matcher = pattern.matcher(ISBN); while (matcher.find()) System.out.println(matcher.group()); ............ it remains for you to create your own regulars for the data that you want to pull out and forward - Alexey Shimansky

1 answer 1

To parse strings that have the same format, just use the java.util.regex package, create the correct regular expression by pulling out the necessary data. Pattern and Matches are responsible for pattern and matcher, respectively.

As an example:

 String ISBN = "ISBN: 123-456-789-112-3, ISBN: 1234567891123"; Pattern pattern = Pattern.compile("(\\d-?){13}"); Matcher matcher = pattern.matcher(ISBN); while (matcher.find()) System.out.println(matcher.group()); 

pull out

 123-456-789-112-3 1234567891123 

All results will be in the form of lines. Therefore, after drawing up the regulars correctly, if you need to convert to a type, then we use the appropriate methods for these classes. For example:

 String testString = "666"; int testInteger = Integer.parseInt(testString); 

or

 String testString = "666.666"; double testDouble = Double.parseDouble(testString); 

If the fractional number has a comma 666,666 , and a dot separator is needed, then you can use java.text.NumberFormat

 NumberFormat format = NumberFormat.getInstance(Locale.FRANCE); Number number = format.parse("1,234"); double d = number.doubleValue();