# RegEx to capture quoted text

**URL:** <https://forum.xojo.com/t/regex-to-capture-quoted-text/17066>\
**Category:** General\
**Created:** [June 5, 2014, 8:44pm UTC](https://forum.xojo.com/t/regex-to-capture-quoted-text/17066 "2014-06-05T20:44:07Z")\
**Posts on this page:** 5\
**Page:** 1

<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:** [June 5, 2014, 8:44pm UTC](https://forum.xojo.com/t/regex-to-capture-quoted-text/17066/1 "2014-06-05T20:44:07Z")

</div>

Let’s say you have a string that contains quoted text, and you need to capture that text including escaped quotes. For example, if the escape character is a backslash, let’s say the text is:

```auto
Title: "not \"the one\", but the one."
```

You want to end up with `not \"the one\", but the one.`. This pattern will do that:

```auto
"((?:\\\"|[^"])+)"
```

(The quotes are part of the pattern. In Xojo, you’d enter that as `"""((?:\\\""|[^""])+)"""`.)

It start with the quote then starts a capturing group. Another non-capturing group is started that looks for either the backslash-quote or another character that is not a quote, and that’s repeated one or more times. Finally, the ending quote is captured. The text will be within SubExpressionString( 1 ).

---

<div class="post-metadata">

**Author:** ![Mike\_Cotrone](https://forum.xojo.com/user_avatar/forum.xojo.com/mike_cotrone/32/19940_2.png) [@Mike\_Cotrone](https://forum.xojo.com/u/Mike_Cotrone)\
**Post date:** [June 5, 2014, 8:53pm UTC](https://forum.xojo.com/t/regex-to-capture-quoted-text/17066/2 "2014-06-05T20:53:17Z")

</div>

[quote=95921:@Kem Tekinay]Let’s say you have a string that contains quoted text, and you need to capture that text including escaped quotes. For example, if the escape character is a backslash, let’s say the text is:

```auto
Title: "not \"the one\", but the one."
```

You want to end up with `not \"the one\", but the one.`. This pattern will do that:

```auto
"((?:\\\"|[^"])+)"
```

(The quotes are part of the pattern. In Xojo, you’d enter that as `"""((?:\\\""|[^""])+)"""`.)

It start with the quote then starts a capturing group. Another non-capturing group is started that looks for either the backslash-quote or another character that is not a quote, and that’s repeated one or more times. Finally, the ending quote is captured. The text will be within SubExpressionString( 1 ).[/quote]  
Awesome Kem and thank you for the tip. RegEx can get complicated fast! 🙂

I have always seen the capture groups but I haven’t used them. Now I am wondering if I should have been 🙂  
Normally I would come up with a pattern like this to get the same results. Do you think this way is ok (scalable)?

Note: The pattern is from RegExRx and not Xojo so my quotes are as such.

```auto
(?<=").+?(?=")
```

Thanks!

---

<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:** [June 5, 2014, 9:02pm UTC](https://forum.xojo.com/t/regex-to-capture-quoted-text/17066/3 "2014-06-05T21:02:56Z")

</div>

You are using lookaround there (coincidentally what my latest xDev column covers), and the problem is in properly getting the next match. For example:

```auto
"part 1" filler "part 2"
```

What do you expect to capture? Most likely you want “part 1” and “part 2”, but what you’ll get is “part 1”, " filler ", and “part 2”. Lookarounds don’t advance the internal pointer so the first match will leave the pointer before the final quote, not after it, so it will be counted again.

Also, your pattern doesn’t take into account escaped quotes, but you probably knew that.

---

<div class="post-metadata">

**Author:** ![Mike\_Cotrone](https://forum.xojo.com/user_avatar/forum.xojo.com/mike_cotrone/32/19940_2.png) [@Mike\_Cotrone](https://forum.xojo.com/u/Mike_Cotrone)\
**Post date:** [June 5, 2014, 9:38pm UTC](https://forum.xojo.com/t/regex-to-capture-quoted-text/17066/4 "2014-06-05T21:38:26Z")

</div>

[quote=95942:@Kem Tekinay]You are using lookaround there (coincidentally what my latest xDev column covers), and the problem is in properly getting the next match. For example:

```auto
"part 1" filler "part 2"
```

What do you expect to capture? Most likely you want “part 1” and “part 2”, but what you’ll get is “part 1”, " filler ", and “part 2”. Lookarounds don’t advance the internal pointer so the first match will leave the pointer before the final quote, not after it, so it will be counted again.

Also, your pattern doesn’t take into account escaped quotes, but you probably knew that.[/quote]  
Wow Kem I am so glad I asked! Thank you for that info!

---

<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, 5:15pm UTC](https://forum.xojo.com/t/regex-to-capture-quoted-text/17066/5 "2020-10-29T17:15:31Z")

</div>


