# Grouping RegexMatch

**URL:** https://discourse.julialang.org/t/grouping-regexmatch/28509
**Category:** New to Julia
**Tags:** regex, stats
**Created:** [September 7, 2019, 6:33pm UTC](https://discourse.julialang.org/t/grouping-regexmatch/28509 "2019-09-07T18:33:46Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![hasanOryx](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hasanoryx/32/5373_2.png) [@hasanOryx](https://discourse.julialang.org/u/hasanOryx)
#### Post date: [September 7, 2019, 6:33pm UTC](https://discourse.julialang.org/t/grouping-regexmatch/28509/1 "2019-09-07T18:33:46Z")

</div>

I’m trying to read this file, and find how many time the following words appear:  
file:

```nohighlight
What's in a name? That which we call a rose
By any other name would smell as sweet! rose :)

```

Words to be found: `rose' and `sweet` so I wrote the below code:

```julia
fname = "simplefile.txt"
s = read(fname, String)
rx = r"(rose|sweet)s?"
collect(eachmatch(rx, s, overlap = true))

```

And got the output as:

```julia
3-element Array{RegexMatch,1}:
 RegexMatch("rose", 1="rose")  
 RegexMatch("sweet", 1="sweet")
 RegexMatch("rose", 1="rose") 

```

As noticed the word “rose” appeared twice, and in the `collect` appeared in 2 different lines, how can I write a code to made the results be something like:

```bash
rose => 2 times, 
sweet => 1 time

```

And what if I need to remove the duplication or multiple appearance, so that the result be something like:

```bash
found words: rose, sweet

```

---

<div class="post-metadata">

### Author: ![kevbonham](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kevbonham/32/216165_2.png) [@kevbonham](https://discourse.julialang.org/u/kevbonham)
#### Post date: [September 8, 2019, 12:37am UTC](https://discourse.julialang.org/t/grouping-regexmatch/28509/2 "2019-09-08T00:37:12Z")

</div>

> [@hasanOryx](#):
>
> how can I write a code to made the results be something like:
> 
> ```julia
> rose => 2 times, 
> sweet => 1 time
> 
> ```

Check out [countmap](http://juliastats.github.io/StatsBase.jl/stable/counts/#StatsBase.countmap) from StatsBase

> [@hasanOryx](#):
>
> so that the result be something like:
> 
> ```julia
> found words: rose, sweet
> 
> ```

`unique()`?

---

<div class="post-metadata">

### Author: ![hasanOryx](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hasanoryx/32/5373_2.png) [@hasanOryx](https://discourse.julialang.org/u/hasanOryx)
#### Post date: [September 8, 2019, 4:30am UTC](https://discourse.julialang.org/t/grouping-regexmatch/28509/3 "2019-09-08T04:30:45Z")

</div>

Thanks,  
I sorted the results obtained by `unique()` as `sort!(unique!(my_array))`

How can I sort the array obtained by `countmap(my_array)`, I tried `sort(countmap(my_array))` but got it sorted by `key` while I want it to be sorted by `value`

One more point, I noticed for the `unique()` lower case is considered different than upper case, I tried using [titlecase](https://pkg.julialang.org/docs/julia/THl1k/1.1.1/base/strings.html#Base.Unicode.titlecase) as `unique(titlecase(my_array))` but it failed!

---

<div class="post-metadata">

### Author: ![Simon\_Bolland](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simon_bolland/32/8920_2.png) [@Simon\_Bolland](https://discourse.julialang.org/u/Simon_Bolland)
#### Post date: [September 8, 2019, 11:38am UTC](https://discourse.julialang.org/t/grouping-regexmatch/28509/4 "2019-09-08T11:38:44Z")

</div>

You can sort the result of countmap by value using

> `sort(collect(my_array),by=last)`

For titlecase try

> `unique(titlecase.(my_array))`
