![]() |
RegExp & VB Noob Qs
On Fri, 27 Jun 2008 06:30:33 -0400, Mike
wrote: Hi all. I've found a litle set of scripts to use regex in Excel, but there's a snafu. Here's the meat: Set allMatches = REObject.Execute(StringToSearch) ReDim result( 0 to allMatches.Count - 1) For i = 0 to allMatches.Count - 1 result( i ) = allmatches( i ) .Value next i and so on,,, Typically I want to find a substring consisting of the first match plus a small range of the following chararcters(any characters), the second match and a range after that, etc. Something like this: vbscript( "the battlestar pegasus was run by a nutcase, and the battestar galactic was a rustbucket", "battlestar.{0,30}") What I want to get is "battletstar pegasus was run by" and battlestar galatica was a rustbucket", or similar. The problem I run into is when the tokens are close together. Then I somehow lose the second match: vbscript( "the battlestar p and the battestar g are filled with cylons", "battlestar.{0,16}") In this case I get "battlestar p and", and then nothing for the second match. It's as if the second match causes the first match to truncate somehow. Does the object make a fixed range for each match or something? Thx. I suspect the "meat" is in your regex scrips and/or your data, and not in what you posted. It may be in how you define your "small range of the following characters". It might also have to do with typos in your data -- there are several typos in what you've posted and, if these were copied from your data, the regex's probably would not behave as you expect. --ron |
RegExp & VB Noob Qs
<snip
Thx. I suspect the "meat" is in your regex scrips and/or your data, and not in what you posted. It may be in how you define your "small range of the following characters". It might also have to do with typos in your data -- there are several typos in what you've posted and, if these were copied from your data, the regex's probably would not behave as you expect. --ron ron Yeah, it's jut a quick snippet w/o the benefit of copy & paste (at 4 am). My work doesn't allow any kind of i/o. Allow me to make some sense. I didn't see (b4 posting) that there's a whole lot of regex activity in here! Let's say my target string is the following: "Frodo went over to Frodo's house and ate Frodo's food." What I'm searching for is every instance of 'Frodo', with a chunk of following text to give it some context. I'm actually looking for instances of a common term whose meaning can be determined by the following words. By displaying a sort of phrase the user can quickly see if the term is being used in the context that interests him. The search pattern is "Frodo.{0,16}". The resultant match values are : "Frodo went over to Fr" and "Frodo's house and ate" and "Frodo's food." It works fine with an example like this. However if I have a source str where the patterns are close together, I 'lose' the subsequent matches. For example, "Frodo wore Frodo's pants." will result in a match collection size of 2, with the first value "Frodo wore Frodo's pa", and no second value. The only solution I can see is to get the positions of the matches, and extract the text using substring. |
RegExp & VB Noob Qs
On Fri, 27 Jun 2008 12:21:12 -0400, Mike
wrote: <snip Thx. I suspect the "meat" is in your regex scrips and/or your data, and not in what you posted. It may be in how you define your "small range of the following characters". It might also have to do with typos in your data -- there are several typos in what you've posted and, if these were copied from your data, the regex's probably would not behave as you expect. --ron ron Yeah, it's jut a quick snippet w/o the benefit of copy & paste (at 4 am). My work doesn't allow any kind of i/o. Allow me to make some sense. I didn't see (b4 posting) that there's a whole lot of regex activity in here! Let's say my target string is the following: "Frodo went over to Frodo's house and ate Frodo's food." What I'm searching for is every instance of 'Frodo', with a chunk of following text to give it some context. I'm actually looking for instances of a common term whose meaning can be determined by the following words. By displaying a sort of phrase the user can quickly see if the term is being used in the context that interests him. The search pattern is "Frodo.{0,16}". The resultant match values are : "Frodo went over to Fr" and "Frodo's house and ate" and "Frodo's food." It works fine with an example like this. However if I have a source str where the patterns are close together, I 'lose' the subsequent matches. For example, "Frodo wore Frodo's pants." will result in a match collection size of 2, with the first value "Frodo wore Frodo's pa", and no second value. The only solution I can see is to get the positions of the matches, and extract the text using substring. Well, the following regex, with the appropriate options set, will return all strings that start with Frodo and end either with the next Frodo or with the end of a line: Frodo.*?(?=Frodo|$) Here are the options I set: ..Global = True ..IgnoreCase = True ..MultiLine = True ..Pattern = "Frodo.*?(?=Frodo|$)" --ron |
All times are GMT +1. The time now is 04:22 PM. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com