首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用逗号手动输入页面并使用Regex进行范围负号

用逗号手动输入页面并使用Regex进行范围负号
EN

Stack Overflow用户
提问于 2013-07-07 14:34:38
回答 2查看 159关注 0票数 0

我试着从页面的逗号检测中修改或更新代码,

下面的代码显示了如何使用逗号输入页面,

样本: 1,2,5,3,8,它不接受0,或大于最大页数

我想要的是添加这样的代码:

2-5,8,9

8,9,2-5

2-5,8-10

也就是说,打印的页数是2,3,4,5,8,9,10

,但它不会接受像2-5,4,8,9这样的输入,因为2-5中已经使用了4。

如果这可能很难,那么可以输入一个简单的范围,比如: 2-5,没有逗号,所以用户不能输入逗号,如果用户想输入逗号,那么-sign也不能输入。

代码语言:javascript
复制
''CODED By: Chris, Combined to MackieChan solution    
Public Function isCELLPageNumb(ByRef valyo As String, ByVal origMaxPage As Integer) As Boolean
   Dim rgxNumberWithComma As New System.Text.RegularExpressions.Regex("^([0-9]+,?)+$")
   Dim match = rgxNumberWithComma.Match(valyo)
   If Not match.Success Then
      Return False
   Else

      Dim numbers As New List(Of Integer) 'will store added numbers
      For Each Item In valyo.Split(","c)
         Dim intValue As Integer
         'Check if number is a valid integer
         'Check if number is 0
         'Check if number has already added the number list
         'Check if number is greater that MaxPage
         If Not Integer.TryParse(Item, intValue) _
         OrElse intValue > origMaxPage _
         OrElse intValue = 0 _
         OrElse numbers.Contains(intValue) Then
            Return False
         Else
            'Item is valid, continue
            numbers.Add(intValue)
         End If
      Next
   End If
   Return True
End Function   


Private Sub DbGridPapers_CellEndEdit(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DbGridPapers.CellEndEdit

   Dim pagestoprint As String = Nothing

   Try
      pagestoprint = DbGridPapers.Rows(e.RowIndex).Cells(1).Value.ToString
   Catch ex As Exception
   End Try

   If (e.ColumnIndex = 1) And (pagestoprint IsNot Nothing) Then    
      If Not isCELLPageNumb(DbGridPapers.Rows(e.RowIndex).Cells(1).Value, OrigPage(e.RowIndex)) Then
         MyThreadedControl(lbltest, "Text", "INVALID INPUT FOR [PAGES] AT ROW " & (e.RowIndex).ToString)
         DbGridPapers.Rows(e.RowIndex).Cells(1).Value = OrigPage(e.RowIndex)
         Return
      Else
         MyThreadedControl(lbltest, "Text", "The Maximum Page is:" & OrigPage(e.RowIndex).ToString)
      End If

      Dim pageDest As String = Nothing
      If Me.btnpaperpay.Enabled Then
         pageDest = DbGridPapers.Rows(e.RowIndex).Tag & "\"
      Else
         pageDest = docPrintnationPath & "\"
      End If

      Dim filename As String = pageDest & DbGridPapers.Rows(e.RowIndex).HeaderCell.Value.ToString
      Dim OldRegularPrice As Decimal = DbGridPapers.Rows(e.RowIndex).Cells(3).Value
      Dim FILEpages As New List(Of Integer)

      ''IF , AND - CAN BE MIX TO GET THE PAGE THEN ITS BETTER, AND I HAVE TO UPDATE THE CODE HERE ALSO.

      If pagestoprint.Split(",").Length > 1 Then 'Split Length is +1 based
         Dim pageFILES() As String = pagestoprint.Split(",")
         For Each filePids As Integer In pageFILES
            FILEpages.Add(filePids) ''GET range in comma sample page1,page3,page8,page2
         Next
      ElseIf pagestoprint.Split("-").Length > 1 Then 'Split Length is +1 based
         Dim pageFILES() As String = pagestoprint.Split("-")              
         For page As Integer = pageFILES(0) To pageFILES(1)    
            FILEpages.Add(page) ''GET range sample pages2 to page5
         Next
      Else
         Dim pages As Integer
         If (Integer.TryParse(pagestoprint, pages)) Then
            If pages = OrigPage(e.RowIndex) Then
               DbGridPapers.Rows(e.RowIndex).Cells(2).Value = OrigImage(e.RowIndex)
               DbGridPapers.Rows(e.RowIndex).Cells(3).Value = OrigPay(e.RowIndex)
               DbGridPapers.Rows(e.RowIndex).Cells(4).Value = OrigCountedImage(e.RowIndex)
               GoTo pCounter ''Return Original Cells Value
            Else
               FILEpages.Add(pages)''GET single page only
            End If
          End If
       End If   

pCounter:
       Dim paperToTpay As Decimal = txtpapertotpay.Text.Substring(0, txtpapertotpay.Text.LastIndexOf(" ")) 
       paperToTpay -= OldRegularPrice
       paperToTpay += DbGridPapers.Rows(e.RowIndex).Cells(3).Value
       MyThreadedControl(txtpapertotpay, "Text", paperToTpay.ToString & " dollar(s)")
    End If 
End Sub

我觉得很难。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-07-07 15:47:41

我知道自定义算法是可以接受的。在这里,您可以使用一种方法来解释所描述的所有条件:

代码语言:javascript
复制
Private Function extractPages(ByVal inputString As String) As List(Of Integer)
    Dim outList As List(Of Integer) = New List(Of Integer)

    If (inputString.Contains(",")) Then
        outList = extractCommas(inputString, outList)
    ElseIf (inputString.Contains("-")) Then
        outList = extractDashes(inputString, outList)
    End If

    If (outList.Count > 0) Then
        For i As Integer = outList.Count - 1 To 0 Step -1
            If (outList.IndexOf(outList(i)) <> outList.LastIndexOf(outList(i))) Then
                'Repeated item
                'It can be just deleted or shall the function return an error?
                outList.RemoveAt(i)
            End If
        Next
    End If

    Return outList
End Function

Private Function extractCommas(ByVal inputString As String, curList As List(Of Integer)) As List(Of Integer)

    If (inputString.Contains(",")) Then
        Dim temp() As String = inputString.Split(",")
        For Each item In temp
            If (Not item.Contains("-") And IsNumeric(item)) Then
                If (Convert.ToInt32(item.Trim()) > 0) Then
                    curList.Add(Convert.ToInt32(item.Trim()))
                End If
            ElseIf (item.Contains("-")) Then
                curList = extractDashes(item.Trim(), curList)
            End If
        Next
    End If

    Return curList
End Function

Private Function extractDashes(ByVal inputString As String, curList As List(Of Integer)) As List(Of Integer)

    If (inputString.Contains("-")) Then
        Dim temp() = inputString.Split("-")
        If (temp.Length = 2) Then
            If (Convert.ToInt32(temp(0)) <= Convert.ToInt32(temp(1))) Then
                Dim count As Integer = Convert.ToInt32(temp(0)) - 1
                If (count < 0) Then
                    count = 0
                End If
                Do
                    count = count + 1
                    curList.Add(count)
                Loop While (count < Convert.ToInt32(temp(1)))
            End If
        End If
    End If

    Return curList
End Function

您可以调用extractPages并获取所有的页码:

代码语言:javascript
复制
Dim InputString As String = "2-5, 4,8-10"
Dim allPages As List(Of Integer) = extractPages(InputString) 'It returns 2, 3, 4, 5, 8, 9, 10
票数 1
EN

Stack Overflow用户

发布于 2013-07-07 15:35:33

描述

不太清楚您在寻找什么,但是这个powershell解决方案显示了我如何处理问题的逻辑,以便用户输入0,2-5,4,8,9,从而忽略零和额外的冗余数字。

示例

代码语言:javascript
复制
$string = "0,2-5,4,8,9"

[hashtable]$hashPages = @{}

foreach ($chunk in $String -split ",") {

    # if the string has a dash then process it as a range
    if ($chunk -match "(\d+)-(\d+)") {
        # itterate through all the pages in the range
        foreach ($Page in $Matches[1] .. $Matches[2]) {
            # insert this page into a hash, which will keep the numbers unique
            $hashPages[[string]$Page] = $true
            } # next page
        } # end if

    # if string is only a number then process it as a single number
    if ($chunk -match "(\d+)") {
        # insert this page into a hash, which will keep the numbers unique
        $hashPages[[string]$Matches[1]] = $true
        } # end if

    } # next chunk

# remove the undesireable numbers like zero if they were added
$hashPages.Remove("0");

Write-Host "these pages where requested:" $(($hashPages.Keys | sort ) -join ",")

输出

代码语言:javascript
复制
these pages where requested: 2,3,4,5,8,9
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17513067

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档