# RegEx Help

**URL:** <https://forum.xojo.com/t/regex-help/45015>\
**Category:** General\
**Created:** [August 26, 2018, 7:56am UTC](https://forum.xojo.com/t/regex-help/45015 "2018-08-26T07:56:36Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![James\_Dooley](https://forum.xojo.com/user_avatar/forum.xojo.com/james_dooley/32/164_2.png) [@James\_Dooley](https://forum.xojo.com/u/James_Dooley)\
**Post date:** [August 26, 2018, 7:56am UTC](https://forum.xojo.com/t/regex-help/45015/1 "2018-08-26T07:56:36Z")

</div>

RegEx is my weakness, so Im not even sure if this possible in a single go, but what I need to do is replace all the commas within brackets with hash signs, while not touching any other comma in the string, as in:

- f gh (a, b) c, d =\>. f gh (A# b) c, d
- (a, b, c), d, e,. =\>. (A# b# c), d, e
- , m n cab (a, b, c, d) g h ,. =\>. M n cab (A# b# c# d) g h ,

And so on.

Any help much appreciate.

---

<div class="post-metadata">

**Author:** ![Kem\_Tekinay](https://forum.xojo.com/user_avatar/forum.xojo.com/kem_tekinay/32/154_2.png) [@Kem\_Tekinay](https://forum.xojo.com/u/Kem_Tekinay)\
**Post date:** [August 26, 2018, 2:16pm UTC](https://forum.xojo.com/t/regex-help/45015/2 "2018-08-26T14:16:36Z")

</div>

The problem is, there is an indeterminate number of commas within the parens so, technically, this isn’t a pattern the way RegEx understands it. You could certainly right a pattern that identifies such commas, replaces them, then starts from the opening paren again in a loop, but I expect this will be faster:

```auto
Public Function ReplaceCommaInParen(s As String) as String
  dim chars() as string = s.Split( "" )
  dim inParen as boolean
  
  for i as integer = 0 to chars.Ubound
    dim thisChar as string = chars( i )
    
    if thisChar = "(" then
      inParen = true
    elseif thisChar = ")" then
      inParen = false
    elseif thisChar = "," and inParen then
      chars( i ) = "#"
    end if
  next
  
  return join( chars, "" )
End Function
```

---

<div class="post-metadata">

**Author:** ![DaveS](https://forum.xojo.com/letter_avatar_proxy/v4/letter/d/848f3c/32.png) [@DaveS](https://forum.xojo.com/u/DaveS)\
**Post date:** [August 26, 2018, 2:22pm UTC](https://forum.xojo.com/t/regex-help/45015/4 "2018-08-26T14:22:00Z")

</div>

I propose that Kem get his own Forum Section dedicated to RegEx 🙂

---

<div class="post-metadata">

**Author:** ![Bernardo\_Monsalve](https://forum.xojo.com/letter_avatar_proxy/v4/letter/b/d07c76/32.png) [@Bernardo\_Monsalve](https://forum.xojo.com/u/Bernardo_Monsalve)\
**Post date:** [August 26, 2018, 2:22pm UTC](https://forum.xojo.com/t/regex-help/45015/5 "2018-08-26T14:22:52Z")

</div>

[code]Dim strToChange As String= “f gh (a, b) c, d”+ EndOfLine+ “(a, b, c), d, e,”+ EndOfLine+ “, m n cab (a, b, c, d) g h ,”

Dim rg as New RegEx  
Dim rgm as RegExMatch

rg.SearchPattern= “\([a-zA-Z\s\,]\*\)”  
rgm= rg.search(strToChange)

While rgm\<\> Nil  
Dim substr As String= rgm.SubExpressionString(0)  
strToChange= strToChange.ReplaceAll(substr, substr.ReplaceAll(",", “#”))

```
rgm= rg.Search

```

Wend  
[/code]

---

<div class="post-metadata">

**Author:** ![James\_Dooley](https://forum.xojo.com/user_avatar/forum.xojo.com/james_dooley/32/164_2.png) [@James\_Dooley](https://forum.xojo.com/u/James_Dooley)\
**Post date:** [August 26, 2018, 4:26pm UTC](https://forum.xojo.com/t/regex-help/45015/6 "2018-08-26T16:26:36Z")

</div>

Thanks all. I had managed to get as far as a pattern to pick out all the blocks and was using the replace function to the substitution. But my ignorance of the topic led me to believe all that was needed was one more little step…

Enjoy your Sunday evening.

---

<div class="post-metadata">

**Author:** ![system](https://forum.xojo.com/uploads/default/original/1X/455b4dcc0e61630e9f81940004ddffa49c432a46.png) [@system](https://forum.xojo.com/u/system)\
**Post date:** [October 29, 2020, 7:20pm UTC](https://forum.xojo.com/t/regex-help/45015/7 "2020-10-29T19:20:50Z")

</div>


