1
+ package com.baeldung.extractnumber
2
+
3
+ import org.junit.jupiter.api.Assertions
4
+ import org.junit.jupiter.api.Test
5
+ import org.junit.jupiter.params.ParameterizedTest
6
+ import org.junit.jupiter.params.provider.CsvSource
7
+ import java.math.BigInteger
8
+
9
+ fun extractUsingRegex (str : String ): BigInteger ? {
10
+ return Regex (" \\ d+" ).find(str)?.value?.toBigIntegerOrNull()
11
+ }
12
+
13
+ fun extractMultipleUsingRegex (str : String ): List <BigInteger > {
14
+ return Regex (" \\ d+" ).findAll(str).map { it.value.toBigInteger() }.toList()
15
+ }
16
+
17
+ fun extractNumbersUsingSplitAndRegex (str : String ): List <BigInteger > {
18
+ return str.split(Regex (" \\ D+" ))
19
+ .filter { it.isNotBlank() }
20
+ .map { it.toBigInteger() }
21
+ }
22
+
23
+ fun extractNumbersUsingLoop (str : String ): List <BigInteger > {
24
+ val numbers = mutableListOf<BigInteger >()
25
+ val currentNumber = StringBuilder ()
26
+
27
+ for (char in str) {
28
+ if (char.isDigit()) {
29
+ currentNumber.append(char)
30
+ } else if (currentNumber.isNotEmpty()) {
31
+ numbers.add(currentNumber.toString().toBigInteger())
32
+ currentNumber.clear()
33
+ }
34
+ }
35
+
36
+ if (currentNumber.isNotEmpty()) {
37
+ numbers.add(currentNumber.toString().toBigInteger())
38
+ }
39
+
40
+ return numbers
41
+ }
42
+
43
+
44
+ class NumberExtractUnitTest {
45
+
46
+ @ParameterizedTest
47
+ @CsvSource(
48
+ " string with 123 and 333 in the text, 123" ,
49
+ " another string with 456 and 789, 456" ,
50
+ " string 123-234, 123" ,
51
+ " no numbers," ,
52
+ " 3 4 50 numbers6, 3" ,
53
+ " 123456789large numbers0, 123456789" ,
54
+ " 91234567891011121314151617181920number,91234567891011121314151617181920"
55
+ )
56
+ fun `extract first occurrence of number from string using regex` (str : String , expected : String? ) {
57
+ val number = extractUsingRegex(str)
58
+ Assertions .assertEquals(number, expected?.toBigIntegerOrNull())
59
+ }
60
+
61
+ @ParameterizedTest
62
+ @CsvSource(
63
+ " string with 123 and 333 in the text, 123:333" ,
64
+ " another string with 456 and 789, 456:789" ,
65
+ " string 123-234, 123:234" ,
66
+ " no numbers," ,
67
+ " 3 4 50 numbers6, 3:4:50:6" ,
68
+ " 123456789large numbers0, 123456789:0" ,
69
+ " 91234567891011121314151617181920number,91234567891011121314151617181920"
70
+ )
71
+ fun `extract all occurrences of numbers from string using regex` (str : String , expected : String? ) {
72
+ val numbers = extractMultipleUsingRegex(str)
73
+ val expectedList = expected?.split(" :" )?.map { it.toBigInteger() } ? : emptyList()
74
+ Assertions .assertEquals(expectedList, numbers)
75
+ }
76
+
77
+ @ParameterizedTest
78
+ @CsvSource(
79
+ " string with 123 and 333 in the text, 123:333" ,
80
+ " another string with 456 and 789, 456:789" ,
81
+ " string 123-234, 123:234" ,
82
+ " no numbers," ,
83
+ " 3 4 50 numbers6, 3:4:50:6" ,
84
+ " 123456789large numbers0, 123456789:0" ,
85
+ " 91234567891011121314151617181920number,91234567891011121314151617181920"
86
+ )
87
+ fun `extract all occurrences of numbers from string using split and regex` (str : String , expected : String? ) {
88
+ val numbers = extractNumbersUsingSplitAndRegex(str)
89
+ val expectedList = expected?.split(" :" )?.map { it.toBigInteger() } ? : emptyList()
90
+ Assertions .assertEquals(expectedList, numbers)
91
+ }
92
+
93
+ @ParameterizedTest
94
+ @CsvSource(
95
+ " string with 123 and 333 in the text, 123:333" ,
96
+ " another string with 456 and 789, 456:789" ,
97
+ " string 123-234, 123:234" ,
98
+ " no numbers," ,
99
+ " 3 4 50 numbers6, 3:4:50:6" ,
100
+ " 123456789large numbers0, 123456789:0" ,
101
+ " 91234567891011121314151617181920number,91234567891011121314151617181920"
102
+ )
103
+ fun `extract all occurrences of numbers from string using loop` (str : String , expected : String? ) {
104
+ val numbers = extractNumbersUsingLoop(str)
105
+ val expectedList = expected?.split(" :" )?.map { it.toBigInteger() } ? : emptyList()
106
+ Assertions .assertEquals(expectedList, numbers)
107
+ }
108
+
109
+ @Test
110
+ fun `check empty string scenario for loop` () {
111
+ val numbers = extractNumbersUsingLoop(" " )
112
+ Assertions .assertIterableEquals(numbers, listOf<BigInteger >())
113
+ }
114
+
115
+ @Test
116
+ fun `check empty string scenario for split` () {
117
+ val numbers = extractNumbersUsingSplitAndRegex(" " )
118
+ Assertions .assertIterableEquals(numbers, listOf<BigInteger >())
119
+ }
120
+
121
+ @Test
122
+ fun `check empty string scenario for regex` () {
123
+ val numbers = extractMultipleUsingRegex(" " )
124
+ Assertions .assertIterableEquals(numbers, listOf<BigInteger >())
125
+ }
126
+
127
+ }
0 commit comments