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

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
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,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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