Skip to content

Commit

Permalink
Added extension methods to Regex and CharSequence (#17)
Browse files Browse the repository at this point in the history
* Added Regex.remove(), Regex.removeFirst(), Regex.removeLast() and unit tests

* Added CharSequence.remove(), CharSequence.removeFirst(), CharSequence.removeLast() and unit tests
  • Loading branch information
markwhitaker authored Mar 30, 2020
1 parent 49451fa commit 79aa70e
Show file tree
Hide file tree
Showing 4 changed files with 431 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/main/kotlin/extensions/CharSequenceExtensions.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package uk.co.mainwave.regextoolboxkotlin.extensions

/**
* Remove all matches of the supplied [regex] from this string, returning the modified string.
* If no match for the Regex is found in this string, an unmodified copy of this string is returned.
*/
fun CharSequence.remove(regex: Regex): String = regex.remove(this)

/**
* Remove the first match of the supplied [regex] from this string, returning the modified string.
* If no match for the Regex is found in this string, an unmodified copy of this string is returned.
*/
fun CharSequence.removeFirst(regex: Regex): String = regex.removeFirst(this)

/**
* Remove the last match of the supplied [regex] from this string, returning the modified string.
* If no match for the Regex is found in this string, an unmodified copy of this string is returned.
*/
fun CharSequence.removeLast(regex: Regex): String = regex.removeLast(this)
24 changes: 24 additions & 0 deletions src/main/kotlin/extensions/RegexExtensions.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package uk.co.mainwave.regextoolboxkotlin.extensions

/**
* Remove all matches of this [Regex] from the supplied [input] string, returning the modified string.
* If no match for the Regex is found in the input string, the input string is returned unmodified.
*/
fun Regex.remove(input: CharSequence): String = this.replace(input, "")

/**
* Remove the first match of this [Regex] from the supplied [input] string, returning the modified string.
* If no match for the Regex is found in the input string, the input string is returned unmodified.
*/
fun Regex.removeFirst(input: CharSequence): String = this.replaceFirst(input, "")

/**
* Remove the last match of this [Regex] from the supplied [input] string, returning the modified string.
* If no match for the Regex is found in the input string, the input string is returned unmodified.
*/
fun Regex.removeLast(input: CharSequence): String = this.findAll(input).let { matches ->
when (matches.any()) {
true -> input.removeRange(matches.last().range)
false -> input
}.toString()
}
194 changes: 194 additions & 0 deletions src/test/kotlin/extensions/CharSequenceExtensionsTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
package uk.co.mainwave.regextoolboxkotlin.extensions

import org.junit.Assert
import org.junit.Test
import uk.co.mainwave.regextoolboxkotlin.RegexQuantifier
import uk.co.mainwave.regextoolboxkotlin.regex

class CharSequenceExtensionsTest {
@Test
fun `GIVEN input string containing one regex match WHEN remove() THEN match is removed from string`() {
// Given
val input = "Hello there friendly world"
val regex = regex {
whitespace()
text("there")
}

// When
val actualResult = input.remove(regex)

// Then
Assert.assertEquals("Hello friendly world", actualResult)
}

@Test
fun `GIVEN input string containing one regex match WHEN removeFirst() THEN match is removed from string`() {
// Given
val input = "Hello there friendly world"
val regex = regex {
whitespace()
text("there")
}

// When
val actualResult = input.removeFirst(regex)

// Then
Assert.assertEquals("Hello friendly world", actualResult)
}

@Test
fun `GIVEN input string containing one regex match WHEN removeLast() THEN match is removed from string`() {
// Given
val input = "Hello there friendly world"
val regex = regex {
whitespace()
text("there")
}

// When
val actualResult = input.removeLast(regex)

// Then
Assert.assertEquals("Hello friendly world", actualResult)
}

@Test
fun `GIVEN input string containing multiple regex matches WHEN remove() THEN all matches are removed from string`() {
// Given
val input = "Hello there friendly world"
val regex = regex {
whitespace()
letter(RegexQuantifier.OneOrMore)
}

// When
val actualResult = input.remove(regex)

// Then
Assert.assertEquals("Hello", actualResult)
}

@Test
fun `GIVEN input string containing multiple regex matches WHEN removeFirst() THEN first match is removed from string`() {
// Given
val input = "Hello there friendly world"
val regex = regex {
whitespace()
letter(RegexQuantifier.OneOrMore)
}

// When
val actualResult = input.removeFirst(regex)

// Then
Assert.assertEquals("Hello friendly world", actualResult)
}

@Test
fun `GIVEN input string containing multiple regex matches WHEN removeLast() THEN last match is removed from string`() {
// Given
val input = "Hello there friendly world"
val regex = regex {
whitespace()
letter(RegexQuantifier.OneOrMore)
}

// When
val actualResult = input.removeLast(regex)

// Then
Assert.assertEquals("Hello there friendly", actualResult)
}

@Test
fun `GIVEN input string containing no regex matches WHEN remove() THEN string is returned unaltered`() {
// Given
val input = "Hello there friendly world"
val regex = regex {
digit(RegexQuantifier.OneOrMore)
}

// When
val actualResult = input.remove(regex)

// Then
Assert.assertEquals("Hello there friendly world", actualResult)
}

@Test
fun `GIVEN empty input string WHEN remove() THEN empty string is returned`() {
// Given
val input = ""
val regex = regex {
digit(RegexQuantifier.OneOrMore)
}

// When
val actualResult = input.remove(regex)

// Then
Assert.assertEquals("", actualResult)
}

@Test
fun `GIVEN input string containing no regex matches WHEN removeFirst() THEN string is returned unaltered`() {
// Given
val input = "Hello there friendly world"
val regex = regex {
digit(RegexQuantifier.OneOrMore)
}

// When
val actualResult = input.removeFirst(regex)

// Then
Assert.assertEquals("Hello there friendly world", actualResult)
}

@Test
fun `GIVEN empty input string WHEN removeFirst() THEN empty string is returned`() {
// Given
val input = ""
val regex = regex {
digit(RegexQuantifier.OneOrMore)
}

// When
val actualResult = input.removeFirst(regex)

// Then
Assert.assertEquals("", actualResult)
}

@Test
fun `GIVEN input string containing no regex matches WHEN removeLast() THEN string is returned unaltered`() {
// Given
val input = "Hello there friendly world"
val regex = regex {
digit(RegexQuantifier.OneOrMore)
}

// When
val actualResult = input.removeLast(regex)

// Then
Assert.assertEquals("Hello there friendly world", actualResult)
}

@Test
fun `GIVEN empty input string WHEN removeLast() THEN empty string is returned`() {
// Given
val input = ""
val regex = regex {
digit(RegexQuantifier.OneOrMore)
}

// When
val actualResult = input.removeLast(regex)

// Then
Assert.assertEquals("", actualResult)
}
}
Loading

0 comments on commit 79aa70e

Please sign in to comment.