user3972104
user3972104

Reputation:

Regex replacement , pattern matching

Am using the following code

  Dim pattern As String = "(a\.? |on\.?)"
  dim input as string ="a creation on"
  dim output as String=Regex.Replace(input, pattern, "  ") 

It will give me the output : creat

but my expected output was: creation, can anyone suggest me how can i achieve the expected output?

Or suggest me that how can i remove stand alone words specified in the pattern from the input.

Upvotes: 2

Views: 101

Answers (2)

Avinash Raj
Avinash Raj

Reputation: 174826

Just add a space before on in your regex and replace the matched characters with an empty string.

Dim pattern As String = "(a\.? | on\.?)"

Upvotes: 1

Bruno
Bruno

Reputation: 4665

Dim ResultString As String
Try
    ResultString = Regex.Replace(SubjectString, "(^|\s)(a|on|etc)($|\s)", "  ", RegexOptions.IgnoreCase Or RegexOptions.Multiline)
Catch ex As ArgumentException
    'Syntax error in the regular expression
End Try

Upvotes: 0

Related Questions