3

Suppose you have a string http://server.com/s123456 and want to get 123456 from it. Or http://server.com/s123456&a=100 and get 123456, 100. Many different situations where you want to have parts of a string.

In order to match strings I use regular expressions.

@Test
fun test() {
    val text = "http://server.com/s123456" // Source string.
    val reg = "^http://server.com/s\\d+\$".toRegex()
    assertEquals(true, reg.matches(text))

    // Try to split the string with findAll().
    val a: Sequence<MatchResult> = reg.findAll(text, 0)
    val names = a.map { it.groupValues[0] }.joinToString()
    println(names) // http://server.com/s123456
}

But it doesn't split the source string into chars and digits, just prints: http://server.com/s123456. Is there a way to write a mask, so that we can retrieve numbers (or any other parts) from the string?

3
  • 1
    Im not familiar with kotlin, but You seems to not having groups. "^http://server.com/s(\\d+)\$" try this and use 1 in groupValues index. Commented Feb 17, 2020 at 14:44
  • 1
    @Eraklon, thanks, it works! Please, create an answer here, so that I could accept it. Commented Feb 17, 2020 at 14:57
  • 1
    Cool, glad it helped. Commented Feb 17, 2020 at 14:59

3 Answers 3

1

You need to use capture group around the the part that you interested. Use this "^http://server.com/s(\\d+)\$" and use it.groupValues[1].

Sign up to request clarification or add additional context in comments.

Comments

1

You can try just (\d+)

Example (written in Python):

a = 'http://server.com/s123456'
b = 'http://server.com/s123456&a=100'
c = 'http://server.com/s122&&&'
d = 'http://server.com/s1234!56&a=100terere'
lis=[a,b,c,d]
for item in lis:
    print(re.findall(r'(\d+)', item ))

output

['123456']
['123456', '100']
['122']
['1234', '56', '100']

Comments

1

Thanks to @Eraklon and @moys I wrote a similar answer.

@Test
fun test1() {
    val text = "http://server.com/s123456"
    val reg = "^http://server.com/s(\\d+)\$".toRegex()
    val a = reg.findAll(text, 0)
    val names = a.map { it.groupValues[1] }.joinToString()
    println(names) 
    // 123456
    assertEquals(true, reg.matches(text))
}

@Test
fun test2() {
    val a = "http://server.com/s123456"
    val b = "http://server.com/s123456&a=100"
    val c = "http://server.com/s122&&&"
    val d = "http://server.com/s1234!56&a=100terere"
    val e = "http://server.com/s"
    val f = "http://server.com"
    val reg = "(\\d+)".toRegex()

    val list = listOf(a, b, c, d, e, f)
    list.map { item ->
        val groups: Sequence<MatchResult> = reg.findAll(item, 0)
        val names = groups.map { it.groupValues[1] }.joinToString()
        println(names)
    }
    // Output:
    // 123456
    // 123456, 100
    // 122
    // 1234, 56, 100
    // 
    // 

    val groups: Sequence<MatchResult> = reg.findAll(e, 0)
    val names = groups.map { it.groupValues[1] }.firstOrNull()
    println(names)
    // null
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.