# Regular Expression question

**URL:** https://boards.straightdope.com/t/regular-expression-question/344634
**Category:** Factual Questions
**Created:** [February 15, 2006, 3:43pm UTC](https://boards.straightdope.com/t/regular-expression-question/344634 "2006-02-15T15:43:38Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![FlyingCowOfDoom](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/flyingcowofdoom/32/21776_2.png) [@FlyingCowOfDoom](https://boards.straightdope.com/u/FlyingCowOfDoom)
#### Post date: [February 15, 2006, 3:43pm UTC](https://boards.straightdope.com/t/regular-expression-question/344634/1 "2006-02-15T15:43:38Z")

</div>

Does anyone know of a Regex that will match all spaces that are not within matching quotes? I’m having trouble with this one.

TIA!

–FCOD

---

<div class="post-metadata">

### Author: ![Finagle](https://avatars.discourse-cdn.com/v4/letter/f/b19c9b/32.png) [@Finagle](https://boards.straightdope.com/u/Finagle)
#### Post date: [February 15, 2006, 4:14pm UTC](https://boards.straightdope.com/t/regular-expression-question/344634/2 "2006-02-15T16:14:22Z")

</div>

> [@FlyingCowOfDoom](#):
>
> Does anyone know of a Regex that will match all spaces that are not within matching quotes? I’m having trouble with this one.
> 
> TIA!
> 
> –FCOD

Regular expression syntax is often language specific. So you’d get a better answer if you said what language you are using.

---

<div class="post-metadata">

### Author: ![FlyingCowOfDoom](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/flyingcowofdoom/32/21776_2.png) [@FlyingCowOfDoom](https://boards.straightdope.com/u/FlyingCowOfDoom)
#### Post date: [February 15, 2006, 4:25pm UTC](https://boards.straightdope.com/t/regular-expression-question/344634/3 "2006-02-15T16:25:04Z")

</div>

Never mind. I found an alternate solution.

–FCOD

---

<div class="post-metadata">

### Author: ![CookingWithGas](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/cookingwithgas/32/485_2.png) [@CookingWithGas](https://boards.straightdope.com/u/CookingWithGas)
#### Post date: [February 15, 2006, 4:25pm UTC](https://boards.straightdope.com/t/regular-expression-question/344634/4 "2006-02-15T16:25:59Z")

</div>

I haven’t thought about this long and hard but I think the “matching” part is what will confound you. I am assuming you need to match on both single quotes and double quotes. I do not know of any construct in regex’s that is dynamic, that is, that part of the expression evaluates based on what another part to the left of it matched on.

Are you trying to just grep for this pattern, or are you trying to edit it to make the spaces not in quotes to be something else? There is a convoluted approach you could take for editing them out; I’m not sure if there’s an elegant one.

---

<div class="post-metadata">

### Author: ![friedo](https://avatars.discourse-cdn.com/v4/letter/f/8edcca/32.png) [@friedo](https://boards.straightdope.com/u/friedo)
#### Post date: [February 15, 2006, 4:48pm UTC](https://boards.straightdope.com/t/regular-expression-question/344634/5 "2006-02-15T16:48:04Z")

</div>

> [@CookingWithGas](#):
>
> I haven’t thought about this long and hard but I think the “matching” part is what will confound you. I am assuming you need to match on both single quotes and double quotes. I do not know of any construct in regex’s that is dynamic, that is, that part of the expression evaluates based on what another part to the left of it matched on.

In Perl, you can do such constructions with backreferences. E.g.

```auto

/(['"])(.*?)\1/

```

Would match a ’ or a ", followed by anything, followed by whatever was matched by the first part. If you wanted to filter out all whitespace that was _not_ inside such a construct, well, that’s a real pain in the ass to do with a regex (even in Perl).

---

<div class="post-metadata">

### Author: ![Nature\_s\_Call](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/nature_s_call/32/19006_2.png) [@Nature\_s\_Call](https://boards.straightdope.com/u/Nature_s_Call)
#### Post date: [February 15, 2006, 4:51pm UTC](https://boards.straightdope.com/t/regular-expression-question/344634/6 "2006-02-15T16:51:41Z")

</div>

For all your [RegEx](http://www.regxlib.com/Search.aspx) needs.

---

<div class="post-metadata">

### Author: ![Nature\_s\_Call](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/nature_s_call/32/19006_2.png) [@Nature\_s\_Call](https://boards.straightdope.com/u/Nature_s_Call)
#### Post date: [February 15, 2006, 5:00pm UTC](https://boards.straightdope.com/t/regular-expression-question/344634/7 "2006-02-15T17:00:40Z")

</div>

This will find all spaces not withing single quotes

```auto

(?=(?:[^\']*\'[^\']*\')*(?![^\']*\'))

```

And this will work for double quotes.

```auto

(?=(?:[^\"]*\"[^\"]*\")*(?![^\"]*\")) 

```

---

<div class="post-metadata">

### Author: ![ultrafilter](https://avatars.discourse-cdn.com/v4/letter/u/3d9bf3/32.png) [@ultrafilter](https://boards.straightdope.com/u/ultrafilter)
#### Post date: [February 15, 2006, 6:10pm UTC](https://boards.straightdope.com/t/regular-expression-question/344634/8 "2006-02-15T18:10:19Z")

</div>

It’s actually pretty simple to design the non-deterministic finite state machine that processes this particular language.

```auto

state 0 space state 1
state 0 " state 2
state 0 ' state 3
state 0 other state 0
state 1 [symbol]e[/symbol] state 0
state 2 " state 0
state 2 other state 2
state 3 ' state 0
state 3 other state 3

```

State 0 is the initial state, state 1 is the accepting state (where accepting in this case means performing some action). There are algorithms that can translate this into a regex (depending, of course, on what you want that action to be), but honestly, it’s just as easy to write this one directly.

---

<div class="post-metadata">

### Author: ![CookingWithGas](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/cookingwithgas/32/485_2.png) [@CookingWithGas](https://boards.straightdope.com/u/CookingWithGas)
#### Post date: [February 15, 2006, 6:24pm UTC](https://boards.straightdope.com/t/regular-expression-question/344634/9 "2006-02-15T18:24:04Z")

</div>

> [@friedo](#):
>
> In Perl, you can do such constructions with backreferences. E.g.
> 
> ```auto
> 
> /(['"])(.*?)\1/
> 
> ```
> 
> Would match a ’ or a ", followed by anything, followed by whatever was matched by the first part. If you wanted to filter out all whitespace that was _not_ inside such a construct, well, that’s a real pain in the ass to do with a regex (even in Perl).

I haven’t seen backreferences in the search expression before, I have just seen that used in the replacement string. Is that standard regex or specific to perl?

---

<div class="post-metadata">

### Author: ![rkts](https://avatars.discourse-cdn.com/v4/letter/r/c4cdca/32.png) [@rkts](https://boards.straightdope.com/u/rkts)
#### Post date: [February 15, 2006, 7:37pm UTC](https://boards.straightdope.com/t/regular-expression-question/344634/10 "2006-02-15T19:37:11Z")

</div>

> [@CookingWithGas](#):
>
> I haven’t seen backreferences in the search expression before, I have just seen that used in the replacement string. Is that standard regex or specific to perl?

Backreferences are a standard part of basic regexes. Note, however, that they tend to compromise performance considerably, so it is probably good that OP found an alternate solution.
