0

I am working on a question bank generator using vb.net. Each question has 4 options which starts with a tab and 1),2)3),4)

I am using [^\(|\d][1-4]\), "#" which replaces 1),2),3),4)(option numbers) with #

But it also replaces 1), 2), 3),4) when ever it comes in the Question and Options with #.

Example

17.  Specific heat of certain element is 0.064, its approximate atomic mass is
    1) 100 U    2) 10 U 3) 20 U 4)  Both  2) and 3)

Output is

17. Specific heat of certain element is 0.064, its approximate atomic mass is
    # 100 U # 10 U  # 20 U  #  Both # and #

Expected

 17. Specific heat of certain element is 0.064, its approximate atomic mass is
    # 100 U # 10 U  # 20 U  #  Both 2) and 3)
The fourth bird
  • 154,723
  • 16
  • 55
  • 70
  • Maybe you should separate the model from the view. You could store the questionnaire as XML instead of plain text. E.g. an option would be represented in a structured way as `` and it would be displayed as `1) 100 U`. This makes it a lot easier to read the attributes by using the classes form the [System.Xml.Linq Namespace](https://learn.microsoft.com/en-us/dotnet/api/system.xml.linq?view=net-5.0). – Olivier Jacot-Descombes Jan 16 '21 at 16:39
  • Also see my answer to [Questionnaire database design - which way is better?](https://dba.stackexchange.com/a/11970/6257). – Olivier Jacot-Descombes Jan 16 '21 at 16:47

1 Answers1

1

One option is to use a positive lookbehind to assert a tab directly to the left, then match a digit 1-4 followed by a )

In the replacement use a #

(?<=\t)[1-4]\)
  • (?<=\t) Positive lookbehind, match a tab directly to the left
  • [1-4] Match a digit 1 to 4
  • \) Match a ) char

.NET Regex demo | Vb.net demo

Dim input As String = "17.   Specific heat of certain element is 0.064, its approximate atomic mass is" & vbCrLf &
"1) 100 U   2) 10 U 3) 20 U 4)  Both  2) and 3)"
Dim regex As Regex = New Regex("(?<=\t)[1-4]\)")
Dim result As String = regex.Replace(input, "#")

Output

17.  Specific heat of certain element is 0.064, its approximate atomic mass is
1) 100 U    # 10 U  # 20 U  #  Both  2) and 3)

Console.WriteLine(result) Console.WriteLine(result)

enter image description here


Another option is to match a tab, 1+ digits and the ). In the replacement user a tab and #

\t\d+\)

Vb.net demo

The fourth bird
  • 154,723
  • 16
  • 55
  • 70