Regex: match all spaces except for those after an exact match of a single word or two words

pvr928

Well-known Member
Joined
Oct 21, 2002
Messages
790
Hi

I am trying to match all white spaces in a sentence, except for those after either a match on a particular word, or that particular word followed by another particular word.

I'm trying to use either lookahead or lookbehind. I can match the space immediately preceding the particular word, but not all the other spaces in the sentence.

Examples of the two sentences are (spaces to be matched are noted by { }; spaces not to be matched are noted by [ ] ):

Hello{ }there{ }where{ }is{ }my{ }toy[ ]today{ }Daddy

Hello{ }there{ }where{ }is{ }my{ }toy[ ]soldier[ ]today{ }Daddy

I am using:

Code:
(?=\s).(\btoy\b|\btoy soldier\b)

but this lookahead pattern matches only the space preceding 'toy'.

The two words are 'toy' and 'soldier'. 'toy' will always be present; if 'toy soldier' is present, 'soldier' will always follow 'toy', and 'soldier' will never appear by itself.

Any help greatly appreciated.

Cheers

pvr928
 
Last edited:

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
Since the vba implementation of regex does not have a "look behind", I would probably just have a space as my pattern & have the code otherwise eliminate the unwanted spaces from the text being tested.
So something like this
Code:
Sub TestMatchingSpaces()
  Dim RX As Object, Matches As Object
  Dim s As String
  
  s = "Hello there where is my toy today Daddy"
'  s = "Hello there where is my toy soldier today Daddy"
  
  Set RX = CreateObject("VBScript.RegExp")
  RX.Global = True
  RX.Pattern = " "
  Set Matches = RX.Execute(Replace(Replace(s, "soldier ", "soldier-", 1, -1, 1), "toy ", "toy-", 1, -1, 1))
End Sub
 
Upvote 0
Hi @Peter_SSs

Thanks for your code. I think I've bitten off more Regex than I can chew, so I'll incorporate what's expressed in your solution, being a replacement of the relevant whitespaces in the string with another non-word character, and then search for the remaining whitespaces.

Cheers

pvr928
 
Upvote 0

Forum statistics

Threads
1,215,013
Messages
6,122,690
Members
449,092
Latest member
snoom82

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top