T
teo
[Regular Expression] match word with interpunctuation
Hallo,
I need to build a regular expression
able to match a specified word,
preceded and followed by few chars (mainly interpunctuation)
Below the code.
------------------------
Explanation:
let's assume the word is
baby
these are the 27 cases I need to match:
baby baby is simply standing alone (obviously)
and also these cases:
baby.
baby,
baby;
baby:
baby!
baby?
baby baby with a following blank space
baby-
baby"
baby'
baby\
baby/
baby)
and also these cases:
..Baby
,baby
;baby
:baby
!baby
?baby
baby baby with a preceding blank space
-baby
"baby
'baby
\baby
/baby
(baby
and also
the resulting regexp
must exclude the following cases:
babylon baby followed by any char
lonbaby baby preceded by any char
baby678 baby followed by any number
678baby baby precede by any number
-------------------
This is the code:
'I have to count all the the occurences in a long text.
'I tried this code, but it didn't work:
Dim myCounter As Integer = 0
Dim myMatch As Match = Nothing
Dim myLongText As String = "This morning ...blah...blah.... goodnight to
everyone"
Dim myPattern As String = "[.;:/,-"!\?'( ]" & "baby" & "[.;:/,-"!\?') ]"
myMatch = Regex.Match(myLongText, myPattern, RegexOptions.Multiline Or
RegexOptions.IgnoreCase)
Do While myMatch.Success
myCounter = myCounter + 1
myMatch = myMatch.NextMatch
Loop
Debug.Write (myCounter.tostring)
Hallo,
I need to build a regular expression
able to match a specified word,
preceded and followed by few chars (mainly interpunctuation)
Below the code.
------------------------
Explanation:
let's assume the word is
baby
these are the 27 cases I need to match:
baby baby is simply standing alone (obviously)
and also these cases:
baby.
baby,
baby;
baby:
baby!
baby?
baby baby with a following blank space
baby-
baby"
baby'
baby\
baby/
baby)
and also these cases:
..Baby
,baby
;baby
:baby
!baby
?baby
baby baby with a preceding blank space
-baby
"baby
'baby
\baby
/baby
(baby
and also
the resulting regexp
must exclude the following cases:
babylon baby followed by any char
lonbaby baby preceded by any char
baby678 baby followed by any number
678baby baby precede by any number
-------------------
This is the code:
'I have to count all the the occurences in a long text.
'I tried this code, but it didn't work:
Dim myCounter As Integer = 0
Dim myMatch As Match = Nothing
Dim myLongText As String = "This morning ...blah...blah.... goodnight to
everyone"
Dim myPattern As String = "[.;:/,-"!\?'( ]" & "baby" & "[.;:/,-"!\?') ]"
myMatch = Regex.Match(myLongText, myPattern, RegexOptions.Multiline Or
RegexOptions.IgnoreCase)
Do While myMatch.Success
myCounter = myCounter + 1
myMatch = myMatch.NextMatch
Loop
Debug.Write (myCounter.tostring)