Skip to content

Commit

Permalink
Add 'group' combinator
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisPenner committed Sep 9, 2019
1 parent 1b7a342 commit 25c15cd
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 8 deletions.
3 changes: 3 additions & 0 deletions ChangeLog.md
@@ -1,5 +1,8 @@
# Changelog for lens-regex-pcre

# UNRELEASED 0.3.2.0
Add 'group = groups . ix n' for accessing a single group.

# 0.3.1.0
Match -> Match text
Added regexBS to run regex on ByteStrings directly
Expand Down
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -18,7 +18,7 @@ txt :: Text
txt = "raindrops on roses and whiskers on kittens"

-- Search
λ> has (regex [rx|whisk|]) txt
λ> has (regex [rx|whisk|] . match) txt
True

-- Get matches
Expand Down
7 changes: 4 additions & 3 deletions lens-regex-pcre.cabal
@@ -1,10 +1,10 @@
cabal-version: 1.12

-- This file has been generated from package.yaml by hpack version 0.31.1.
-- This file has been generated from package.yaml by hpack version 0.31.2.
--
-- see: https://github.com/sol/hpack
--
-- hash: c1baf100f3d2d1c413b88695a96d578b0a8d18aace0929759a5428dcfa30b1f1
-- hash: 3bb90fc0aad7064897c98047f082798825404f2187c6a0ac1901de2412b29f3e

name: lens-regex-pcre
version: 0.3.1.0
Expand Down Expand Up @@ -37,12 +37,12 @@ library
ghc-options: -Wall
build-depends:
base >=4.7 && <5
, bytestring
, lens
, pcre-heavy
, pcre-light
, template-haskell
, text
, bytestring
default-language: Haskell2010

test-suite lens-regex-pcre-test
Expand All @@ -55,6 +55,7 @@ test-suite lens-regex-pcre-test
ghc-options: -Wall -threaded -rtsopts -with-rtsopts=-N
build-depends:
base >=4.7 && <5
, bytestring
, hspec
, lens
, lens-regex-pcre
Expand Down
22 changes: 19 additions & 3 deletions src/Control/Lens/Regex.hs
Expand Up @@ -19,9 +19,10 @@ module Control.Lens.Regex
, regexBS
, match
, groups
, group
, matchAndGroups

-- * Compiling regex
-- * Compiling regexes
, rx
, mkRegexQQ
, compile
Expand All @@ -32,7 +33,7 @@ module Control.Lens.Regex
, Regex
) where

import qualified Data.Text as T hiding (index)
import qualified Data.Text as T
import qualified Data.Text.Encoding as T
import qualified Data.Text.Encoding.Error as T
import qualified Data.ByteString as BS
Expand Down Expand Up @@ -70,7 +71,7 @@ type GroupRanges = [(Int, Int)]
-- >>> "raindrops on roses and whiskers on kittens" ^.. regex [rx|(\w+) on (\w+)|] . groups
-- [["raindrops","roses"],["whiskers","kittens"]]
--
-- You can access a specific group by combining with `ix`
-- You can access a specific group combining with 'ix', or just use 'group' instead
--
-- >>> "raindrops on roses and whiskers on kittens" ^.. regex [rx|(\w+) on (\w+)|] . groups . ix 1
-- ["roses","kittens"]
Expand All @@ -92,6 +93,21 @@ type GroupRanges = [(Int, Int)]
groups :: Traversal' (Match text) [text]
groups = partsOf (traversed . _Right)

-- | Access a specific group of a match. Numbering starts at 0.
--
-- See 'groups' for more info on grouping
--
-- >>> "key:value, a:b" ^.. regex [rx|(\w+):(\w+)|] . group 0
-- ["key","a"]
--
-- >>> "key:value, a:b" ^.. regex [rx|(\w+):(\w+)|] . group 1
-- ["value","b"]
--
-- >>> "key:value, a:b" & regex [rx|(\w+):(\w+)|] . group 1 %~ T.toUpper
-- "key:VALUE, a:B"
group :: Int -> Traversal' (Match text) text
group n = groups . ix n

-- | Traverse each match
--
-- Get a match if one exists:
Expand Down
2 changes: 1 addition & 1 deletion stack.yaml
Expand Up @@ -17,7 +17,7 @@
#
# resolver: ./custom-snapshot.yaml
# resolver: https://example.com/snapshots/2018-01-01.yaml
resolver: lts-13.24
resolver: lts-14.1

# User packages to be built.
# Various formats can be used as shown in the example below.
Expand Down
12 changes: 12 additions & 0 deletions stack.yaml.lock
@@ -0,0 +1,12 @@
# This file was autogenerated by Stack.
# You should not edit this file by hand.
# For more information, please see the documentation at:
# https://docs.haskellstack.org/en/stable/lock_files

packages: []
snapshots:
- completed:
size: 523448
url: https://raw.githubusercontent.com/commercialhaskell/stackage-snapshots/master/lts/14/1.yaml
sha256: 0045b9bae36c3bb2dd374c29b586389845af1557eea0423958d152fc500d4fbf
original: lts-14.1
9 changes: 9 additions & 0 deletions test/Spec.hs
Expand Up @@ -109,6 +109,15 @@ main = hspec $ do
("raindrops on roses and whiskers on kittens" & regex [rx|(\w+) on (\w+)|] . groups %~ reverse)
`shouldBe` "roses on raindrops and kittens on whiskers"

describe "group" $ do
it "should get a single group" $ do
"a:b c:d" ^.. regex [rx|(\w):(\w)|] . group 1
`shouldBe` ["b", "d"]

it "should set a single group" $ do
"a:b c:d" & regex [rx|(\w):(\w)|] . group 1 %~ T.toUpper
`shouldBe` "a:B c:D"

describe "traversed" $ do
it "should allow setting all group matches" $ do
("one two three" & regex [rx|(\w+) (\w+)|] . groups . traversed .~ "new")
Expand Down

0 comments on commit 25c15cd

Please sign in to comment.