Regex 使用正则表达式通过逗号和范围负号手动输入页面
我正在尝试更改或更新检测页面逗号的代码 下面的代码显示了如何使用逗号输入页面 示例:1,2,5,3,8,不接受0或大于最大页数 我要求的是添加可以接受的代码 像这样: 2-5,8,9Regex 使用正则表达式通过逗号和范围负号手动输入页面,regex,vb.net,Regex,Vb.net,我正在尝试更改或更新检测页面逗号的代码 下面的代码显示了如何使用逗号输入页面 示例: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并且没有逗号,所以用户如果想输入,就不能输入逗号 逗号,则无法同时输入-符号 ''CODED By:
或
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并且没有逗号,所以用户如果想输入,就不能输入逗号 逗号,则无法同时输入-符号
''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
我认为这很难。描述
现在还不清楚您在寻找什么,但这个powershell解决方案展示了我如何解决问题的逻辑,允许用户输入0,2-5,4,8,9
,从而忽略零位和多余的冗余数字
示例
$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 ",")
输出
these pages where requested: 2,3,4,5,8,9
我知道自定义算法是可以接受的。这里有一种方法可以解释所有描述的情况:
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
您是否接受基于自定义字符串分析而不是基于正则表达式的解决方案?如果您对代码进行注释以帮助其他人理解它,这将非常有用。另外,您的前两个示例文本没有包含
10
,因此它们与所需的输出不完全一致。好的,我得到了代码,但是,如果我输入一个字符串,它似乎没有错误捕获。如果有9个页面,那么我输入10,它接受不好的数字。我真正想要的是在“isCELLPageNumb”函数中添加一些接受签名的代码。我已经制作了这样的代码,它只允许通过它设计用来处理的特定条件。它将忽略除由数字和/或“-”和/或“,”组成的字符串以外的任何内容。在目前的版本中,它已经是高度可靠的;在任何情况下,我都没有分析每一个可能的场景:首先是因为您更了解可能输入的内容,其次是因为(正如1分钟前在其他地方所说的那样),我不打算为其他人解决问题,只是为面临问题的程序员提供一些帮助。我提出的备选方案(超出空间…(继续)与您正在使用的完全不同,这是我打算提供的帮助:以不同的方式面对您的问题,在我看来,这是更具适应性/可扩展性的,等等。您可以按原样使用我的代码(如前所述,已经很好地工作了),但这是我所期望的(/在你的情况下会做的)是你接受它,调整它,甚至改进它;也就是说,用知识/替代方法来面对问题,而不是代码本身。在任何情况下,如果你需要进一步的帮助来理解/改进/扩展我的代码,你可以问。
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