# Regex: ERROR: PCRE error: requested value is not set

**URL:** https://discourse.julialang.org/t/regex-error-pcre-error-requested-value-is-not-set/39170
**Category:** New to Julia
**Tags:** regex
**Created:** [May 9, 2020, 9:29am UTC](https://discourse.julialang.org/t/regex-error-pcre-error-requested-value-is-not-set/39170 "2020-05-09T09:29:36Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![D4taV1s](https://avatars.discourse-cdn.com/v4/letter/d/73ab20/32.png) [@D4taV1s](https://discourse.julialang.org/u/D4taV1s)
#### Post date: [May 9, 2020, 9:29am UTC](https://discourse.julialang.org/t/regex-error-pcre-error-requested-value-is-not-set/39170/1 "2020-05-09T09:29:36Z")

</div>

task: apply regex to a multi-line string each like only some line contain a custom tag  
only apply a substitution to the line that contain that field, and blank out all the other lines (data extraction)

current regex

```julia
.*label="([^"]*?)".*|.*

```

```julia
replace(s, r""".*aria-label="([^"]*?)".*|.*"""m => s"\1")

```

partial (simple) method is to ignore the missed capture object, and to return blank for that,  
and since the half after the “or” is listed second, it blank out the line,

usability simplicity may be factors

how to i do the replace for all occurrences in the string

reference:

```julia
  replace(s::AbstractString, pat=>r; [count::Integer])

  Search for the given pattern pat in s, and replace each occurrence with r. If count is provided, replace at most count occurrences. pat may be a single character, a vector or a set of characters, a string, or a regular expression. If r is a function, each occurrence is
  replaced with r(s) where s is the matched substring (when pat is a Regex or AbstractString) or character (when pat is an AbstractChar or a collection of AbstractChar). If pat is a regular expression and r is a SubstitutionString, then capture group references in r are replaced
  with the corresponding matched text. To remove instances of pat from string, set r to the empty String ("").

  Examples
  ≡≡≡≡≡≡≡≡≡≡

  julia> replace("Python is a programming language.", "Python" => "Julia")
  "Julia is a programming language."
  
  julia> replace("The quick foxes run quickly.", "quick" => "slow", count=1)
  "The slow foxes run quickly."
  
  julia> replace("The quick foxes run quickly.", "quick" => "", count=1)
  "The foxes run quickly."
  
  julia> replace("The quick foxes run quickly.", r"fox(es)?" => s"bus\1")
  "The quick buses run quickly."

```

---

<div class="post-metadata">

### Author: ![tlienart](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tlienart/32/7640_2.png) [@tlienart](https://discourse.julialang.org/u/tlienart)
#### Post date: [May 9, 2020, 9:49am UTC](https://discourse.julialang.org/t/regex-error-pcre-error-requested-value-is-not-set/39170/2 "2020-05-09T09:49:15Z")

</div>

You might want to add an example string with desired output for people to suggest ideas but note that optional capture groups are not properly substituted when they don’t match (see this bug: [Replace with optional matching group fails when there is no match · Issue #31456 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/issues/31456)).

What you might want to do is have an if statement checking if there’s a match and then do the replace (yes it’s not as nice but it’s usually pretty fast anyway)

---

<div class="post-metadata">

### Author: ![D4taV1s](https://avatars.discourse-cdn.com/v4/letter/d/73ab20/32.png) [@D4taV1s](https://discourse.julialang.org/u/D4taV1s)
#### Post date: [May 9, 2020, 10:18am UTC](https://discourse.julialang.org/t/regex-error-pcre-error-requested-value-is-not-set/39170/3 "2020-05-09T10:18:02Z")

</div>

example

```julia
<div A_ID="A_attr.1">
<div B1_tag="B1_attr.1">B1_in</div>
<div B2_tag="B2_attr.1">B2_in</div></div>
<div A_ID="A_attr.2">
<div B1_tag="B1_attr.2">B1_in</div>
<div B2_tag="B2_attr.2">B2_in</div></div>
<div A_ID="A_attr.3">
<div B1_tag="B1_attr.3">B1_in</div>
<div B2_tag="B2_attr.3">B2_in</div></div>

```

```julia
replace(s, r""".*B1_tag="([^"]*?)".*|.*"""m => s"\1")

```

```julia

B1_attr.1

B1_attr.2

B1_attr.3

```

note that line positioning is preserved

Atom Screenshot (below)  
 ![image](https://global.discourse-cdn.com/julialang/original/3X/9/b/9bcc5dc73ff29d42f71c9223e704d85469dc3a97.png)  
Text editors (although they have differences in other places) Atom, VS-Code , Sublime Text  
all replace missed captures with blanks

I appreciate the reply

also: the line positioning in this above case is used later

---

<div class="post-metadata">

### Author: ![tlienart](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tlienart/32/7640_2.png) [@tlienart](https://discourse.julialang.org/u/tlienart)
#### Post date: [May 9, 2020, 10:30am UTC](https://discourse.julialang.org/t/regex-error-pcre-error-requested-value-is-not-set/39170/4 "2020-05-09T10:30:32Z")

</div>

```julia
s = """
   <div A_ID="A_attr.1">
   <div B1_tag="B1_attr.1">B1_in</div>
   <div B2_tag="B2_attr.1">B2_in</div></div>
   <div A_ID="A_attr.2">
   <div B1_tag="B1_attr.2">B1_in</div>
   <div B2_tag="B2_attr.2">B2_in</div></div>
   <div A_ID="A_attr.3">
   <div B1_tag="B1_attr.3">B1_in</div>
   <div B2_tag="B2_attr.3">B2_in</div></div>
   """

io = IOBuffer()
for line in split(s)
   m = match(r""".*B1_tag="([^"]*?)".*""", line)
   if !isnothing(m)
      write(io, m.captures[1])
   else
      write(io, "\n")
   end
end
println(String(take!(io)))

```

Gives

```julia

B1_attr.1

B1_attr.2

B1_attr.3

```

---

<div class="post-metadata">

### Author: ![D4taV1s](https://avatars.discourse-cdn.com/v4/letter/d/73ab20/32.png) [@D4taV1s](https://discourse.julialang.org/u/D4taV1s)
#### Post date: [May 9, 2020, 10:32am UTC](https://discourse.julialang.org/t/regex-error-pcre-error-requested-value-is-not-set/39170/5 "2020-05-09T10:32:40Z")

</div>

Thanks so much!
