J
J
Jsman2016-03-05 12:02:23
Java
Jsman, 2016-03-05 12:02:23

How to pull the first value?

Good afternoon dear experts! So the question is:
We parsed the xls document and go over each line (a certain cell) where the names of people are located in one cell through a space there can be 1 person, or maybe 10. I wrote a regular expression ([A-Z]+\\s[A-Z]+){1}" and checked everything is OK in the regular expression tester, it catches the first person (I didn’t have a Middle Name, only the Last Name and First Name), but in reality it turns out that all the FIs of people come from one cell. Tell me where is the mistake and how to fix it? I’ve been sitting all night and I can’t figure out what needs to be done to get only the first match in each cell.
And the second question is how to calculate the number of tourists in the cells?
Tourists are written in cells like this:
Petrov Ivan
Petrov Ivan
- the number of people can be from one to ...

HSSFWorkbook xlsxOur = new HSSFWorkbook(new FileInputStream("../report/отчет.xls"));
        HSSFSheet listOne = xlsxOur.getSheet("Лист1");

        for(int i=0; i <= listOne.getLastRowNum(); i++){
            String xlsxList = listOne.getRow(i).getCell(1).getStringCellValue();

            Matcher accommodation1 = Pattern.compile("([А-Я]+\\s[А-Я]+){1}").matcher(xlsxList);

            while (accommodation1.find()) {
                String valueGlobal = listOne.getRow(i).getCell(0).getStringCellValue() + " " + accommodation1.group(1);
                System.out.println(valueGlobal);

            }
        }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gornostaev, 2016-03-05
@SeniorDmitry

^ (\w+\s\w+)
or

String[] names = xlsxList.split("\\s+");
String name;
if(names.length() >= 2) name = names[0] + " " + names[1];

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question