# Regular expression How do I find a line that contains this rule but also something else?

**URL:** <https://discourse.julialang.org/t/regular-expression-how-do-i-find-a-line-that-contains-this-rule-but-also-something-else/46010>\
**Category:** General Usage\
**Created:** [September 3, 2020, 1:48pm UTC](https://discourse.julialang.org/t/regular-expression-how-do-i-find-a-line-that-contains-this-rule-but-also-something-else/46010 "2020-09-03T13:48:53Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![programista](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/programista/32/5372_2.png) [@programista](https://discourse.julialang.org/u/programista)\
**Post date:** [September 3, 2020, 1:48pm UTC](https://discourse.julialang.org/t/regular-expression-how-do-i-find-a-line-that-contains-this-rule-but-also-something-else/46010/1 "2020-09-03T13:48:53Z")

</div>

How do I find a line that contains this rule but also something else?

```julia
rx=r"^[0-9]{2}-[0-9]{3}$"
occursin.( rx ,string.(dane[:,6]))

```

Thx, Paul

---

<div class="post-metadata">

**Author:** ![danielw2904](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/danielw2904/32/10890_2.png) [@danielw2904](https://discourse.julialang.org/u/danielw2904)\
**Post date:** [September 3, 2020, 2:04pm UTC](https://discourse.julialang.org/t/regular-expression-how-do-i-find-a-line-that-contains-this-rule-but-also-something-else/46010/2 "2020-09-03T14:04:54Z")

</div>

Could you maybe give an example line? Something else is not very meaningful in this context. At the moment the line is completely described by the rule since you have start and end.

---

<div class="post-metadata">

**Author:** ![evad](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/evad/32/15756_2.png) [@evad](https://discourse.julialang.org/u/evad)\
**Post date:** [September 3, 2020, 3:51pm UTC](https://discourse.julialang.org/t/regular-expression-how-do-i-find-a-line-that-contains-this-rule-but-also-something-else/46010/3 "2020-09-03T15:51:44Z")

</div>

I think he means that the line should strictly contain this regex and something other than the pattern?  
maybe:

```julia
rx = r".+\b[0-9]{2}-[0-9]{3}\b | \b[0-9]{2}-[0-9]{3}\b.+"

```

---

<div class="post-metadata">

**Author:** ![danielw2904](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/danielw2904/32/10890_2.png) [@danielw2904](https://discourse.julialang.org/u/danielw2904)\
**Post date:** [September 3, 2020, 3:57pm UTC](https://discourse.julialang.org/t/regular-expression-how-do-i-find-a-line-that-contains-this-rule-but-also-something-else/46010/4 "2020-09-03T15:57:47Z")

</div>

But this changes the original rule. Maybe `.+` in the middle?

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [September 3, 2020, 4:19pm UTC](https://discourse.julialang.org/t/regular-expression-how-do-i-find-a-line-that-contains-this-rule-but-also-something-else/46010/5 "2020-09-03T16:19:20Z")

</div>

@programista you’ve been around since 2016, you know you have to provide more info here.

What is ‘something else’, what is `dane`?

---

<div class="post-metadata">

**Author:** ![programista](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/programista/32/5372_2.png) [@programista](https://discourse.julialang.org/u/programista)\
**Post date:** [September 3, 2020, 5:54pm UTC](https://discourse.julialang.org/t/regular-expression-how-do-i-find-a-line-that-contains-this-rule-but-also-something-else/46010/6 "2020-09-03T17:54:14Z")

</div>

Like this

```julia
695-element Array{Any,1}:
 "201-215"
 "20-215"
 "Ul. Sobieskiego nr. jeszcze nie ma punkt
 "Kamieńskiego 201-215 Brt"
 "Ul wczasowa 8 82-103"
 "Ul.Klifowa 23 72-350"
 "Kamieńskiego 201-215 Bud"
...

```

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [September 3, 2020, 6:31pm UTC](https://discourse.julialang.org/t/regular-expression-how-do-i-find-a-line-that-contains-this-rule-but-also-something-else/46010/7 "2020-09-03T18:31:50Z")

</div>

Can you please write a bit more? Which parts of which of those lines should be matched?

Try to explain what you want. Show a string that should be matched and explain why. And show a similar line that should _not_ be matched, and explain why.

Please put some more effort into the question. We shouldn’t have to spend a lot of time figuring out what your question means.

---

<div class="post-metadata">

**Author:** ![danielw2904](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/danielw2904/32/10890_2.png) [@danielw2904](https://discourse.julialang.org/u/danielw2904)\
**Post date:** [September 3, 2020, 6:52pm UTC](https://discourse.julialang.org/t/regular-expression-how-do-i-find-a-line-that-contains-this-rule-but-also-something-else/46010/8 "2020-09-03T18:52:34Z")

</div>

This makes me think you might just want to remove `^` and `$`. With `occursin` it should match lines that have the described pattern somewhere  
Or @evad 's answer. 😃

---

<div class="post-metadata">

**Author:** ![yakir12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yakir12/32/297_2.png) [@yakir12](https://discourse.julialang.org/u/yakir12)\
**Post date:** [September 3, 2020, 7:30pm UTC](https://discourse.julialang.org/t/regular-expression-how-do-i-find-a-line-that-contains-this-rule-but-also-something-else/46010/9 "2020-09-03T19:30:38Z")

</div>

I really can’t recommend highly enough this awesome site for sorting all regex related things:

> **[RegExr: Learn, Build, & Test RegEx](https://regexr.com)**
>
> RegExr is an online tool to learn, build, & test Regular Expressions (RegEx / RegExp).

---

<div class="post-metadata">

**Author:** ![danielw2904](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/danielw2904/32/10890_2.png) [@danielw2904](https://discourse.julialang.org/u/danielw2904)\
**Post date:** [September 4, 2020, 12:36pm UTC](https://discourse.julialang.org/t/regular-expression-how-do-i-find-a-line-that-contains-this-rule-but-also-something-else/46010/10 "2020-09-04T12:36:16Z")

</div>

```julia
v = [ "201-215",

"20-215",

"Ul. Sobieskiego nr. jeszcze nie ma punkt",

"Kamieńskiego 201-215 Brt",

"Ul wczasowa 8 82-103",

"Ul.Klifowa 23 72-350",

"Kamieńskiego 201-215 Bud"]

rpost = r"^[0-9]{2}-[0-9]{3}$"

# This leaves no opportunity for "something else"

occursin.(rpost, v) # matches only 2nd

r1 = r"[0-9]{2}-[0-9]{3}"

# Fails to not match rows that do not contain "something else"

occursin.(r1, v) # does not match 3rd

r2 = r".*[0-9]{2}-[0-9]{3}.+"

# Fails to match rows where "something else" is on the left

occursin.(r2, v) # matches 4th and last

r3 = r".+[0-9]{2}-[0-9]{3}.*"

# Fails to match rows where "something else" is on the right

occursin.(r3, v) # does not match 2nd and 3rd

r4 = r".+[0-9]{2}-[0-9]{3}.*|.*[0-9]{2}-[0-9]{3}.+"

# I thing this is the answer

occursin.(r4, v) # does not match 2nd and 3rd but more robust

v2 = [v..., "12-234abc"]

occursin.(r3, v2) # fails to match new string

occursin.(r4, v2) # matches new string

```

---

<div class="post-metadata">

**Author:** ![programista](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/programista/32/5372_2.png) [@programista](https://discourse.julialang.org/u/programista)\
**Post date:** [September 4, 2020, 1:06pm UTC](https://discourse.julialang.org/t/regular-expression-how-do-i-find-a-line-that-contains-this-rule-but-also-something-else/46010/11 "2020-09-04T13:06:38Z")

</div>

Thanks , big lesson !
