0

I have two lines in Array list which contains number

line1 1234 5694 7487
line2 10/02/1992 or 1992

I used different regex to get both the line, but the problem is when I use the regex ([0-9]{4}//s?)([0-9]{4}//s?)([0-9]{4}//n) . It gets the first line cool.

But for checking the line2 I used ([0-9]{2}[/-])?([0-9]{2}[/-])?([0-9]{4}). this regex instead of returning the last line its returning first 4 numbers of the line1.

1 Answers1

0

As stated in the comments below you are using .matches which returns true if the whole string can be matched.

In your pattern ([0-9]{2}[/-])?([0-9]{2}[/-])?([0-9]{4}) it would also match only 4 digits as the first 2 groups ([0-9]{2}[/-])?([0-9]{2}[/-])? are optional due to the question mark ? leaving the 3rd group ([0-9]{4}) able to match 4 digits.

What you might do instead is to use an alternation to either match a date like format where the first 2 parts including the delimiter are optional. Or match 3 times 4 digits.

.*?(?:(?:[0-9]{2}[/-]){2}[0-9]{4}|[0-9]{4}(?:\h[0-9]{4}){2}).*

Explanation

  • .*? Match any character except a newline non greedy
  • (?: Non capturing groupo
    • (?:[0-9]{2}[/-]){2} Repeat 2 times matching 2 digits and / or -
    • [0-9]{4} Match 4 digits
    • | Or
    • [0-9]{4} Match 4 digits
    • (?:\\h[0-9]{4}){2} Repeat 2 times matching a horizontal whitespace char and 4 digits
  • ) Close non capturing group
  • .* Match 0+ times any character except a newline

Regex demo | Java demo

For example

List<String> list = Arrays.asList(
new String[]{
    "10/02/1992 or 1992",
    "10/02/1992",
    "10/1992",
    "02/1992",
    "1992",
    "1234 5694 7487"

    }
);

String regex = ".*?(?:(?:[0-9]{2}[/-]){2}[0-9]{4}|[0-9]{4}(?:\\h[0-9]{4}){2}).*";

for (String str: list) {
    if (str.matches(regex)){
        System.out.println(str);
    }
}

Result

10/02/1992 or 1992
10/02/1992
1234 5694 7487

Note that in your first pattern I think you mean \\s instead of //s.

The \\s will also match a newline. If you want to match a single space you could just match that or use \\h to match a horizontal whitespace character.

The fourth bird
  • 154,723
  • 16
  • 55
  • 70
  • 1
    Thank you for the answer . Let me try and let you know – Aditya Sinha Jun 22 '19 at 08:16
  • @AdityaSinha What exactly is not working? Do you get an error and you also want to match the word line? Is the example data in the regex tester correct and are those values the only values in the string or can they also be in the middle? In that case you can omit the anchors `^$` – The fourth bird Jun 22 '19 at 10:59
  • see I have 5 lines in array list . Of which this two lines are account number and dob. I am using for loop to match each line with the regex and get the matched result. I am using regex to check both account and dob . If the dob is on the top then there is no prob the regex works fine . but when the account number is on the top then it matches the acount regex but for dob it takes the first 4 number of the account number and stops. – Aditya Sinha Jun 22 '19 at 11:28
  • So these are 2 out of 5 lines. What are the other 3? Can you share the code that you tried? – The fourth bird Jun 22 '19 at 11:30
  • @AdityaSinha Perhaps you could use this pattern `(?:[0-9]{2}[/-]){2}[0-9]{4}|[0-9]{4}(?:\\h[0-9]{4}){2}` and use it like in this demo https://ideone.com/Yct4P0 – The fourth bird Jun 22 '19 at 11:38
  • if (list.get(i).matches("([0-9]{2}[/-])?([0-9]{2}[/-])?([0-9]{4})")){ Log.d("DOB: ", list.get(i)); – Aditya Sinha Jun 22 '19 at 12:41
  • @AdityaSinha `.matches()` requires the whole pattern to match. See [this page](https://stackoverflow.com/questions/4450045/difference-between-matches-and-find-in-java-regex) and [this page](https://www.vogella.com/tutorials/JavaRegularExpressions/article.html) I have added a new demo to display the whole line. See https://ideone.com/NmD5CD – The fourth bird Jun 22 '19 at 12:55
  • So if you want to use your code you could also update te pattern to `.*(?:[0-9]{2}[/-]){2}[0-9]{4}|[0-9]{4}(?:\\h[0-9]{4}){2}.*` – The fourth bird Jun 22 '19 at 13:00