VBA regex everything after words

hale

I have the sentence "lorem ipsum dolor marker words blah blah blah"

I need only the words after (but not including) 'marker words'. 'Marker words' are the only words in the sentence that remain the same.

I managed to find (?<=\bmarker words\s)(\w+) which not only doesn't work in vba, but it also returns only one word after the marker words ex: blah but I'm not sure how to extend this to the rest of the string (if I were to work on it outside of Excel). My research says that vba doesn't even support lookbehinds at all so I am stuck.

Wiktor Stribiżew

VBA VBScript regex does not support lookbehind construct. You can only rely on capturing:

\bmarker words\s+(.*)

See the regex demo

The result is in the SubMatches collection.

The \bmarker words\s+ subpattern matches the whole words marker words followed with 1 or more whitespace(s) and (.*) subpattern matches zero or more characters other than a newline and places it into a capture group, a memory buffer, where the value can be further retrieved from with language specific methods.

VBA code:

Sub CaptureSubstring()
  Dim str As String
  Dim objMatches As Object
  str = "lorem ipsum dolor marker words blah blah blah"
  Set objRegExp = CreateObject("VBScript.RegExp")     ' Declare the RegExp object
  objRegExp.Pattern = "\bmarker words\s+(.*)"         ' Set pattern
  Set objMatches = objRegExp.Execute(str)             ' Execute the regex match
  If objMatches.Count <> 0 Then                       ' Check the result
    Debug.Print objMatches.Item(0).SubMatches.Item(0) ' Print Match 1, Submatch 1
    ' > blah blah blah
  End If
End Sub

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

VBA regex everything after words

From Dev

Regex for remove everything after | (with | )

From Dev

Regex to get everything between 2 words

From Dev

clojure regex to match words and everything inbetween

From Dev

Regex text after words

From Dev

RegEx How to get everything after a /

From Dev

regex to match a word and everything after it?

From Dev

Everything after/before regex in Python

From Dev

Using regex to remove everything after "..."

From Dev

regex to match a word and everything after it?

From Dev

RegEx: Remove pattern and everything after it

From Dev

Regular expression to match everything after two words

From Dev

Extracting everything after first two words in R

From Dev

Regex remove everything after Regex Match

From Dev

PHP - Regex replace everything not letter or space and everything after it

From Dev

Regex - Get everything after, but not including, this character

From Dev

Regex match everything after each character

From Dev

Match everything after word using only regex

From Dev

Regex match everything after conditional match

From Dev

Javascript regex remove everything after .extension

From Dev

Obtain everything after one of these phrases in regex?

From Dev

Regex in php, select everything after a part

From Dev

Delete everything after date on Atom using Regex

From Dev

regex - delete everything after the first letter

From Dev

Regex to remove everything after a specific pattern

From Dev

Remove everything after a 10 digit number regex

From Dev

remove everything that is not capital letter words from a string Regex rails

From Dev

Regex Match everything except words of particular flag format

From Dev

Regex Find and Replace: Deleting everything except words starting with "#"?

Related Related

  1. 1

    VBA regex everything after words

  2. 2

    Regex for remove everything after | (with | )

  3. 3

    Regex to get everything between 2 words

  4. 4

    clojure regex to match words and everything inbetween

  5. 5

    Regex text after words

  6. 6

    RegEx How to get everything after a /

  7. 7

    regex to match a word and everything after it?

  8. 8

    Everything after/before regex in Python

  9. 9

    Using regex to remove everything after "..."

  10. 10

    regex to match a word and everything after it?

  11. 11

    RegEx: Remove pattern and everything after it

  12. 12

    Regular expression to match everything after two words

  13. 13

    Extracting everything after first two words in R

  14. 14

    Regex remove everything after Regex Match

  15. 15

    PHP - Regex replace everything not letter or space and everything after it

  16. 16

    Regex - Get everything after, but not including, this character

  17. 17

    Regex match everything after each character

  18. 18

    Match everything after word using only regex

  19. 19

    Regex match everything after conditional match

  20. 20

    Javascript regex remove everything after .extension

  21. 21

    Obtain everything after one of these phrases in regex?

  22. 22

    Regex in php, select everything after a part

  23. 23

    Delete everything after date on Atom using Regex

  24. 24

    regex - delete everything after the first letter

  25. 25

    Regex to remove everything after a specific pattern

  26. 26

    Remove everything after a 10 digit number regex

  27. 27

    remove everything that is not capital letter words from a string Regex rails

  28. 28

    Regex Match everything except words of particular flag format

  29. 29

    Regex Find and Replace: Deleting everything except words starting with "#"?

HotTag

Archive