Xojo Conferences
MBSOct2019CologneDE

Regex Help - subexpressioncount (Real Studio network user group Mailinglist archive)

Back to the thread list
Previous thread: Installation/License Software
Next thread: stringwidth accuracy


When is CompareRows done?   -   Hadley, Joshua
  Regex Help - subexpressioncount   -   jeff benjamin
   Re: Regex Help - subexpressioncount   -   Frank Bitterlich
   Re: Regex Help - subexpressioncount   -   Charles Yeomans
    Re: Regex Help - subexpressioncount   -   jeff benjamin
   Re: Regex Help - subexpressioncount   -   Didier Barbas

Regex Help - subexpressioncount
Date: 29.08.02 16:18 (Thu, 29 Aug 2002 11:18:55 -0400)
From: jeff benjamin
I am trying to see how many times a snippet of text appears within
another snippet:

thecaption="test test test"
rg.SearchPattern="test"
myMatch=rg.search(thecaption)

Should not myMatch.subexpressioncount = 3?

In my testing it is 1.

Thanks,
Jeff

---
Subscribe to the digest:
<mailto:<email address removed>>
Unsubscribe:
<mailto:<email address removed>>

Re: Regex Help - subexpressioncount
Date: 29.08.02 16:24 (Thu, 29 Aug 2002 17:24:21 +0200)
From: Frank Bitterlich
Nope. The Subexpressioncount is not the nomber of occurrences of the
pattern in the search string. It's for the Subexpressions found per
match; for example, a pattern of
(te)(st)
would give you a SubExpressionCount of 2; subexpression 0 is "test" (the
whole match), 1 is "te", and 2 is "st".

It becomes useful with a pattern like this:
(t)(e)*(st)

For the string "test", SubExpressionCount would be 3; but for "teeest",
it would be 5, being "t", "e", "e", "e" and "st".

Cheers,
Frank+++

P.S. I might have got some pattern syntax details wrong, since I'm
currently working with different RegEx versions. Use with care, shake
before use.

jeff benjamin wrote:
>
> I am trying to see how many times a snippet of text appears within
> another snippet:
>
> thecaption="test test test"
> rg.SearchPattern="test"
> myMatch=rg.search(thecaption)
>
> Should not myMatch.subexpressioncount = 3?
>
> In my testing it is 1.

Re: Regex Help - subexpressioncount
Date: 29.08.02 16:33 (Thu, 29 Aug 2002 11:33:59 -0400)
From: Charles Yeomans

On Thursday, August 29, 2002, at 11:18 AM, jeff benjamin wrote:

> I am trying to see how many times a snippet of text appears within
> another snippet:
>
> thecaption="test test test"
> rg.SearchPattern="test"
> myMatch=rg.search(thecaption)
>
> Should not myMatch.subexpressioncount = 3?
>
> In my testing it is 1.
>

You misunderstand the concept of a subexpression. A subexpression comes
from a search string like the following.

"<(w+)>"

This would match "<html>", and the first subexpression is defined by a
match of the search string inside the parentheses. So
SubexpressionCount = 1; subexpressionString(0) = "<html>", and
subexpressionString(1) = "html".

Searching your string for "test" will return the position of the first
match it finds. To count the number of matches in a string, you'll need
to do a loop.

dim matchCount as Integer
dim startingPosition as Integer
dim r as RegEx
dim match as RegExMatch

matchCount= 0
startingPosition = 0
r = new RegEx
r.SearchPattern = "test"
Do
match = r.Search("test test test", startingPosition)
If match <> nil then
matchCount = matchCount + 1
Else
Exit
End if
Loop

Charles Yeomans


---
Subscribe to the digest:
<mailto:<email address removed>>
Unsubscribe:
<mailto:<email address removed>>

Re: Regex Help - subexpressioncount
Date: 29.08.02 18:01 (Thu, 29 Aug 2002 13:01:19 -0400)
From: jeff benjamin
Aha. Guess I did misunderstand the subexpression.

Thanks much.

On Thursday, August 29, 2002, at 11:33 AM, Charles Yeomans wrote:

>
> On Thursday, August 29, 2002, at 11:18 AM, jeff benjamin wrote:
>
>> I am trying to see how many times a snippet of text appears within
>> another snippet:
>>
>> thecaption="test test test"
>> rg.SearchPattern="test"
>> myMatch=rg.search(thecaption)
>>
>> Should not myMatch.subexpressioncount = 3?
>>
>> In my testing it is 1.
>>
> You misunderstand the concept of a subexpression. A subexpression
> comes from a search string like the following.
>
> "<(w+)>"
>
> This would match "<html>", and the first subexpression is defined by a
> match of the search string inside the parentheses. So
> SubexpressionCount = 1; subexpressionString(0) = "<html>", and
> subexpressionString(1) = "html".
>
> Searching your string for "test" will return the position of the first
> match it finds. To count the number of matches in a string, you'll
> need to do a loop.
>
> dim matchCount as Integer
> dim startingPosition as Integer
> dim r as RegEx
> dim match as RegExMatch
>
> matchCount= 0
> startingPosition = 0
> r = new RegEx
> r.SearchPattern = "test"
> Do
> match = r.Search("test test test", startingPosition)
> If match <> nil then
> matchCount = matchCount + 1
> Else
> Exit
> End if
> Loop
>
> Charles Yeomans
>
> ---
> Subscribe to the digest:
> <mailto:<email address removed>>
> Unsubscribe:
> <mailto:<email address removed>>

---
Subscribe to the digest:
<mailto:<email address removed>>
Unsubscribe:
<mailto:<email address removed>>

Re: Regex Help - subexpressioncount
Date: 30.08.02 01:49 (Fri, 30 Aug 2002 08:49:26 +0800)
From: Didier Barbas
Subexpressions, as the name tries to indicate, are sub-expressions, ie small
chunks, isolated by (), within a regular expression (and not within your
text), whenever there is a match. "a match" meaning just that: ONE match.
For all matches, you have to loop, no way around it. Subexpressions(0) is
the whole match. See RBDeveloper issue #1's articles about regexes (Matt
Neuburg's and mine. Start with Matt's...)

dim thecaption as string
dim rg as regEx
dim myMatch as regexMatch
dim i as integer

// make a little project with two editfields
// ~1 for the caption, and ~2 for the test string
thecaptioníitField1.text
rg=new regex
rg.SearchPatterníitField2.text
// your counter
i myMatch=rg.search(thecaption)
while myMatch<>nil
i=i+1
// Achtung! Line breaks! the two following lines are one RB line
myMatch=rg.search(thecaption,myMatch.subExpressionStart(0) +
len(myMatch.subExpressionString(0)))
wend
msgBox str(i)
// displays the number of matches

HTH


Why can't hearing aids be configured with regExes?
preg_replace (/(([bullshit])([good stuff]))*/, / \\3/);
Huh?

--