Results 1 to 5 of 5

Thread: [2008] using the +, ||, and * symbol in regex ruins my text?

  1. #1

    Thread Starter
    Frenzied Member Icyculyr's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    1,934

    [2008] using the +, ||, and * symbol in regex ruins my text?

    I am inserting spaces before and after text, using regex, to make operators be spaced, IE:
    EDIT
    I AM ALSO UNABLE to use the characters "(" and ")"
    if (i>0) {}
    becomes
    if (i > 0) {}

    now it works great, but if I add the characters ||, * or +, it adds like 20 spaces in my text and totally ruins it? I end up with something like
    i f (i>0 ) {}

    etc..

    I am declaring the regex like this:

    Code:
    Dim rRegex As New Regex("\b||\b")
    , as an example

    Does anyone know why?
    Last edited by Icyculyr; Mar 15th, 2008 at 05:25 AM.

  2. #2
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: [2008] using the +, ||, and * symbol in regex ruins my text?

    What code are you using to apply this regex to your text? If possible show some code with input and the output you're getting (in [code] tags so that it preserves whitespace)

  3. #3

    Thread Starter
    Frenzied Member Icyculyr's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    1,934

    Re: [2008] using the +, ||, and * symbol in regex ruins my text?

    Here is all the code for formatting my text, as you will see.

    vb.net Code:
    1. Public Function AutoFormatLines() As Object
    2.         If Not Settings.bFormatText Then
    3.             Return Nothing
    4.         End If
    5.         pbProgress.Visible = True
    6.         Dim iBracketsFound As Integer = 0
    7.         Dim iTabIndent As Integer = Settings.iIndent
    8.         Dim sFoundWhat As String = ""
    9.         bDisableSelectionUpdates = True
    10.         Dim sOperators() As String = {"<", ">", "=", "<=", ">=", "&&", "-", "/", "==", "!="}
    11.         'Cant use the brackets ( or ), and I can't use ||, +, *
    12.         Dim sBrackets() As String = {"(", ")"}
    13.         Dim sBracketsLength As Integer = sBrackets.Length - 1
    14.         'use regex
    15.         Dim sOperatorLength As Integer = sOperators.Length - 1
    16.         Dim rRegex(sOperatorLength) As Regex
    17.         For i As Integer = 0 To sOperatorLength
    18.             rRegex(i) = New Regex("\b" & sOperators(i) & "\b")
    19.         Next i
    20.         Dim rBrackets(sBracketsLength) As Regex
    21.         For i As Integer = 0 To sBracketsLength
    22.             'rBrackets(i) = New Regex("\b" & sBrackets(i) & "\b")
    23.         Next i
    24.         Dim mc As MatchCollection = Nothing
    25.         LockWindowUpdate(rtbEdit.Handle)
    26.         lStatus.Text = "Status: Formatting Text"
    27.         Application.DoEvents()
    28.         'Adjust one line at a time
    29.         'Search for lines that start with a bracket (only)
    30.         Dim iStart As Integer = rtbEdit.SelectionStart
    31.         Dim iLength As Integer = rtbEdit.Lines.Length
    32.         Dim sLinesText() As String = rtbEdit.Lines
    33.         Dim iIndent As Integer = 0
    34.         Dim bPass As Boolean = False
    35.         Dim bSkip As Boolean = False
    36.         pbProgress.PerformStep()
    37.         'Dim sRTBText As String = rtbEdit.Text
    38.         For i As Integer = 0 To iLength - 1
    39.             bPass = False
    40.             bSkip = False
    41.             If i = CInt(iLength / 4) - 1 Then
    42.                 pbProgress.PerformStep()
    43.                 Application.DoEvents()
    44.             End If
    45.             If i = CInt(iLength / 2) - 1 Then
    46.                 pbProgress.PerformStep()
    47.                 Application.DoEvents()
    48.             End If
    49.             If i = CInt(iLength / 1.3) - 1 Then
    50.                 pbProgress.PerformStep()
    51.                 Application.DoEvents()
    52.             End If
    53.             Dim iCurrentLineLength As Integer = sLinesText(i).Length
    54.             If iCurrentLineLength > 0 Then
    55.                 Dim iSelectStart As Integer = rtbEdit.GetFirstCharIndexFromLine(i)
    56.                 Dim iSub As String = sLinesText(i).Substring(0, 1)
    57.                 Dim iTempInteger As Integer = 0
    58.                 Do Until iSub <> " " AndAlso iSub <> ControlChars.Tab 'tabs!
    59.                     Dim iLen As Integer = sLinesText(i).Length
    60.                     If iLen < iTempInteger + 1 Then
    61.                         Exit Do
    62.                     End If
    63.                     iSub = sLinesText(i).Substring(iTempInteger, 1)
    64.                     iTempInteger += 1
    65.                 Loop
    66.                 Dim iTempIndent As Integer = iBracketsFound * iTabIndent
    67.                 Dim iLineN As Integer = sLinesText(i).Length - 1
    68.                 For k As Integer = 0 To iLineN
    69.                     Dim sSubstr As String = sLinesText(i).Substring(k, 1)
    70.                     If sSubstr = "{" Then
    71.                         iBracketsFound += 1
    72.                         sFoundWhat = "{"
    73.                         bPass = True 'SEARCH in this FOR LOOP for >0 and make it > 0 etc..
    74.                     End If
    75.                     If sSubstr = "}" Then
    76.                         iBracketsFound -= 1
    77.                         sFoundWhat = "}"
    78.                         bPass = True
    79.                     End If
    80.                     Dim bRan As Boolean = False
    81.                 Next k
    82.                 For k As Integer = 0 To sOperatorLength
    83.                     mc = rRegex(k).Matches(sLinesText(i))
    84.                     For Each mMatch As Match In mc
    85.                         If mMatch.Index + sOperators(k).Length < sLinesText(i).Length Then
    86.                             Dim sB4 As String = sLinesText(i).Substring(mMatch.Index, sOperators(k).Length)
    87.                             Dim iAFPos As Integer = 0
    88.                             If sB4 <> " " Then
    89.                                 rtbEdit.SelectionStart = iSelectStart + mMatch.Index
    90.                                 rtbEdit.SelectionLength = 0
    91.                                 rtbEdit.SelectedText = " "
    92.                                 'rtbEdit.Text = rtbEdit.Text.Insert(iSelectStart + mMatch.Index, " ")
    93.                                 iAFPos = 1
    94.                             End If
    95.                             If mMatch.Index + iAFPos + sOperators(k).Length < sLinesText(i).Length Then
    96.                                 Dim sAF As String = sLinesText(i).Substring(mMatch.Index + iAFPos, sOperators(k).Length)
    97.                                 If sAF <> " " Then
    98.                                     rtbEdit.SelectionStart = iSelectStart + mMatch.Index + iAFPos + sOperators(k).Length
    99.                                     rtbEdit.SelectionLength = 0
    100.                                     rtbEdit.SelectedText = " "
    101.                                     'rtbEdit.Text = rtbEdit.Text.Insert(iSelectStart + mMatch.Index + iAFPos + sOperators(k).Length, " ")
    102.                                 End If
    103.                             End If
    104.                         End If
    105.                     Next mMatch
    106.                 Next k
    107.                 'For k As Integer = 0 To sBracketsLength
    108.                 '    mc = rBrackets(k).Matches(sLinesText(i))
    109.                 '    For Each mMatch As Match In mc
    110.                 '        If mMatch.Index + 1 < sLinesText(i).Length Then
    111.                 '            Dim sB4 As String = sLinesText(i).Substring(mMatch.Index, 1)
    112.                 '            Dim iAFPos As Integer = 0
    113.                 '            If sB4 = " " Then
    114.                 '                rtbEdit.Text = rtbEdit.Text.Remove(iSelectStart + mMatch.Index, 1)
    115.                 '                iAFPos = 1
    116.                 '            End If
    117.                 '            If mMatch.Index + iAFPos + 1 < sLinesText(i).Length Then
    118.                 '                Dim sAF As String = sLinesText(i).Substring(mMatch.Index + iAFPos, sOperators(k).Length)
    119.                 '                If sAF <> " " Then
    120.                 '                    rtbEdit.Text = rtbEdit.Text.Remove(iSelectStart + mMatch.Index + iAFPos, 1)
    121.                 '                End If
    122.                 '            End If
    123.                 '        End If
    124.                 '    Next mMatch
    125.                 'Next k
    126.                 iIndent = iBracketsFound * iTabIndent
    127.                 Dim iTempIndent2 As Integer = iIndent
    128.                 If iLineN + 1 > 1 Then
    129.                     'this line has more than just a curly bracket!
    130.                     iIndent = iTempIndent ' we also need to move that curly bracket to the next line!
    131.                     If sFoundWhat = "}" Then 'coming down, revert to iIndent
    132.                         iIndent = iTempIndent2
    133.                     End If
    134.                 Else
    135.                     iIndent -= iTabIndent
    136.                 End If
    137.                 If iTempInteger > 0 Then
    138.                     rtbEdit.SelectionStart = rtbEdit.GetFirstCharIndexFromLine(i)
    139.                     rtbEdit.SelectionLength = iTempInteger - 1
    140.                     rtbEdit.SelectedText = ""
    141.                 End If
    142.                 If Not bSkip Then
    143.                     rtbEdit.SelectionStart = iSelectStart
    144.                     For k As Integer = 0 To iIndent - 1
    145.                         rtbEdit.SelectedText &= " "
    146.                     Next k
    147.                     iTabPositions(iTabCount) = iSelectStart + iIndent
    148.                     iTabCount += 1
    149.                 End If
    150.             End If
    151.         Next i
    152.         rtbEdit.SelectionStart = iStart
    153.         bDisableSelectionUpdates = False
    154.         LockWindowUpdate(IntPtr.Zero)
    155.         pbProgress.PerformStep()
    156.         Threading.Thread.Sleep(250)
    157.         lStatus.Text = "Status: Ready"
    158.         pbProgress.Visible = False
    159.         pbProgress.Value = 0
    160.         Return Nothing
    161.     End Function

    Can you see what's wrong?

  4. #4

    Thread Starter
    Frenzied Member Icyculyr's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    1,934

    Re: [2008] using the +, ||, and * symbol in regex ruins my text?

    Here are the errors,
    Trying to use */ in regex (to search for)

    error: An error occurred creating the form. See Exception.InnerException for details. The error is: parsing "*/" - Quantifier {x,y} following nothing.

    trying to use + in regex (to search for)

    An error occurred creating the form. See Exception.InnerException for details. The error is: parsing "+" - Quantifier {x,y} following nothing.

    trying to use * in regex (to search for)

    An error occurred creating the form. See Exception.InnerException for details. The error is: parsing "*" - Quantifier {x,y} following nothing.

    Trying to use ( in regex (to search for)

    An error occurred creating the form. See Exception.InnerException for details. The error is: parsing "(" - Not enough )'s.

    trying to use ) in regex (to search for)

    An error occurred creating the form. See Exception.InnerException for details. The error is: parsing ")" - Too many )'s.



    / adds 3 spaces behind it (instead of 1)
    - same as /
    * puts spaces between almost every letter in my code? IE i f ( 0 > 1 ) i n t i = 0 ; etc....
    || adds about 50 spaces behind it, and deletes most of the text.

    I am just spacing operators, so
    if (i!=0) would become, if (i != 0)
    everything works fine, but the / - * and || symbols, the */ won't compile, and the ( and ) symbols won't compile either..
    They will compile if I place "\b" tags around them, but funny stuff happens..

    Does anyone know why?

    Thanks

  5. #5

    Thread Starter
    Frenzied Member Icyculyr's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    1,934

    Re: [2008] using the +, ||, and * symbol in regex ruins my text?

    perhaps I can wrap another tag around them to make the regex treat it as a letter? and not a regex code?

    Thanks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width