Regular Expression  - "/" - Backslash - Quote Next Char

A simple implementation of the regular expression "/" (Backslash) in Visual Basic. This regular expression is also known as "Quote Next Char."

A backslash quotes any character. This allows a search for a character that is usually a regular expression itself.

In this example, a comma can be part of a phrase being searched for in a string, but it is also a delimiter for phrases in the string. So, the need arises to distinguish between the comma that is part of the phrase and the comma acting as delimiter between phrases. This is done by using the regular expression "/" (backslash). The backslash is placed before the comma in the phrase.

Example:

Search 1: "word A/, and word B form a single phrase"

Search 2: "word A/,, word b are two phrases"


Visual Basic Code

Sub Context_e(IDOutgoing, IDTermSelected)

'

' Context_e Makro

'

' More context words in output for this word tag?

'

' Set default values

PreviousChar = "Y"

CurrentChar = "X"

CurrentStringPos = WordTagPos + 3

StartOfContextWord = WordTagPos + 3

While NoMoreContextWords = False And WordNotInPhrase = True ' (2)

' Get next context word

' While CurrentChar <> "]" And CurrentChar <> "," ' (1)

While (CurrentChar <> "]" And CurrentChar <> ",") Or (CurrentChar <> "]" And CurrentChar = "," And PreviousChar = "/")

' Context word found, get this word

LengthOfContextWord = (CurrentStringPos + 1) - StartOfContextWord

'

' This is wrong if > 1 tag word in output

'

CurrentStringPos = CurrentStringPos + 1

CurrentChar = Mid(Output, CurrentStringPos, 1)

'

' Store previous char

'

PreviousChar = Mid(Output, CurrentStringPos - 1, 1)

Wend ' Get context word (1)

ContextWord = Mid(Output, StartOfContextWord, LengthOfContextWord)

' Context word found

'

' Check if context word/phrase contains "/"

' If so, remove it.

' And get position of "/" in context word

SlashPos = InStr(ContextWord, "/")

If SlashPos <> 0 Then

' "/" found

' Save string up to "/"

FirstPart = Left(ContextWord, SlashPos - 1)

' Save string after "/"

SecondPart = Right(ContextWord, LengthOfContextWord - SlashPos)

' Combine both parts to make new context word

' i.e. without "/"

'

ContextWord = FirstPart + SecondPart

End If

' Is this context word in context phrase?

WordInPhrase = InStr(ContextPhrase, ContextWord)

'

' Store position (Pos0) of tag word in output string

'

Pos0 = WordTagPos + 3

If WordInPhrase = 0 Then

WordNotInPhrase = True

Else

GoTo MatchFound: ' exit loop

End If

'

' If current char is comma, first see if the previous char is "/"

' (Quote Next Char), then comma is part of context word/phrase, else

' point to next char and set CurrentChar = default value

'

If CurrentChar = "," And PreviousChar <> "/" Then

CurrentStringPos = CurrentStringPos + 1

CurrentChar = "X"

StartOfContextWord = StartOfContextWord + LengthOfContextWord + 2

ElseIf CurrentChar = "]" Then

NoMoreContextWords = True

End If

Wend ' Search for more context words (2)

WordTagPos = InStr(CurrentStringPos, Output, "[W:")

NoMoreContextWords = False

Wend ' More tag words in output (3)


Back to Visual Basic Tutorial:

Like some details on the programmer?