Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/henoc/regex-refined
Refinement types for regex
https://github.com/henoc/regex-refined
refinement-types regex scala
Last synced: about 1 month ago
JSON representation
Refinement types for regex
- Host: GitHub
- URL: https://github.com/henoc/regex-refined
- Owner: henoc
- License: gpl-2.0
- Created: 2018-11-06T11:35:00.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2018-11-23T17:28:28.000Z (about 6 years ago)
- Last Synced: 2023-06-30T16:46:36.028Z (over 1 year ago)
- Topics: refinement-types, regex, scala
- Language: Java
- Size: 166 KB
- Stars: 3
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# regex-refined
[![Maven Central](https://img.shields.io/maven-central/v/com.github.henoc/regex-refined_2.12.svg)](http://mvnrepository.com/artifact/com.github.henoc/regex-refined_2.12)
[![License](https://img.shields.io/badge/license-GPL%2BCE-blue.svg)](https://openjdk.java.net/legal/gplv2+ce.html)[refined](https://github.com/fthomas/refined) for regex string, and some type-safe regex utilities.
## build.sbt
```sbt
libraryDependencies += "com.github.henoc" %% "regex-refined" % "0.1.0"
```## Usage
### Refinement
There are more examples in test files.
```scala
import regex_string._
import eu.timepit.refined.api._
import eu.timepit.refined.W
import eu.timepit.refined.generic.Equal
import eu.timepit.refined.auto._// Regex string should have one caputuring-group
val _: String Refined GroupCount[Equal[W.`1`.T]] = "a(b)c"// Regex string should have a group name "integer"
val _: String Refined HasGroupName[W.`"integer"`.T] = "[+-]?((?[0-9]*)[.])?[0-9]+"// Regex string should be full match pattern
val _: String Refined FullMatchPattern = "^abc$"// Regex string should be correct as js-regex
val _: String Refined JsRegex = """abx[\b]cde"""
```### Pattern matching
`r` string interpolator extractor checks the number of groups in compile time.
```scala
"2018-11-18" match {
case r"""(\d+$year)-(\d+$month)-(\d+$day)""" => println(s"year = $year, month = $month, day = $day")
case _ => println("no!")
}
```The variable positions of interpolator is not concerned, so you can also write:
```scala
r"""(\d+)-(\d+)-(\d+)$year$month$day"""
r"""(?x) (\d+)-(\d+)-(\d+) # $year, $month, $day"""
```But this throws compile error:
```scala
r"""(?x) (\d+)-(\d+)-\d+ # $year, $month, $day"""
```