[Regular Expression] match a word with interpunctuation

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • teo

    #1

    [Regular Expression] match a word with interpunctuation

    [Regular Expression] match word with interpunctuatio n

    Hallo,
    I need to build a regular expression
    able to match a specified word,
    preceded and followed by few chars (mainly interpunctuatio n)
    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(myL ongText, myPattern, RegexOptions.Mu ltiline Or
    RegexOptions.Ig noreCase)


    Do While myMatch.Success
    myCounter = myCounter + 1
    myMatch = myMatch.NextMat ch
    Loop



    Debug.Write (myCounter.tost ring)

  • Chris Chilvers

    #2
    Re: [Regular Expression] match a word with interpunctuatio n

    On Tue, 27 Jun 2006 01:58:22 +0200, teo <teo@inwind.i t> wrote:[color=blue]
    >
    >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" & "[.;:/,-"!\?') ]"[/color]

    Does it specificaly have to be that punctuation or can it be any
    non-alphanumeric character?

    If so, some thing like:
    "\b" & word & "\b" should work.

    See "regular expressions, atomic zero-width assertions" in the help.

    Comment

    • teo

      #3
      Re: [Regular Expression] match a word with interpunctuatio n

      >>[color=blue][color=green]
      >>Dim myPattern As String = "[.;:/,-"!\?'( ]" & "baby" & "[.;:/,-"!\?') ]"[/color]
      >
      >Does it specificaly have to be that punctuation or can it be any
      >non-alphanumeric character?
      >
      >If so, some thing like:
      >"\b" & word & "\b" should work.[/color]

      I specificaly have to match that punctuaction.
      It seems I have solved it
      with this regexp:

      [^\w] [\\\,;:!&?("'./- ] ? baby [\\\,;:!&?)"'/.- ] ? [^\w]

      ----------------------------

      I have another question:

      I want to extract a given sequence,

      the sequence occurs many times,

      but I'm satisfied just
      when I've found the very first one,

      so to save computational time,
      I'd like to tell to the RegExp to "stop and exit immediately" the search,
      and give me the sequence,
      I repeat in order to save time.

      Is there any command to append to the RegExp?


      Comment

      Working...