我有一个日期范围从jquery ui daterange选择器输入到文本框中。我需要获得开始日期和结束日期的回发值。这些值在文本框中提供,但我对如何在回发时使用VB服务器端代码分离这些值一无所知。任何人都可以告诉我如何使用vbscript分隔开始和结束日期?文本框结果如下:
{"开始":" 2017年4月12日""端":" 2017年5月17日"}
我尝试使用以下代码,但它不起作用
Dim strDateStart as String
Dim strDateEnd as String
strDateStart = txtSearchDateRange.Text
strDateStart = Replace(strDateStart, "end*", "")
strDateEnd = txtSearchDateRange.Text
strDateEnd = Replace(strDateEnd, "start*", "")
感谢@Mederic,以下代码有效:
Dim value As String = txtSearchDateRange.Text
Dim strStartDate As String = ""
Dim strEndDate As String = ""
Dim i As Integer = 0
' Call Regex.Matches method.
Dim matches As MatchCollection = Regex.Matches(value, "\d{4}-\d{2}-\d{2}")
' Loop over matches.
For Each m As Match In matches
' Loop over captures.
For Each c As Capture In m.Captures
i = i + 1
' Display.
Console.WriteLine("Index={0}, Value={1}", c.Index, c.Value)
If i = 1 Then strStartDate = c.Value
If i = 2 Then strEndDate = c.Value
Next
Next
Response.Write("<BR><BR><BR><BR><BR><BR>Start Date:" & strStartDate & "<BR><BR>End Date:" & strEndDate)
答案 0 :(得分:1)
使用群组进行正则表达式的更简洁方法
第一:
Imports System.Text.RegularExpressions
然后:
'Our regex
Dim regex As Regex = New Regex("(?<start>\d{4}-\d{2}-\d{2}).*(?<end>\d{4}-\d{2}-\d{2})")
'Match from textbox content
Dim match As Match = regex.Match(TextBox1.Text)
'If match is success
If match.Success Then
'Print start group
Console.WriteLine(match.Groups("start").Value)
'Print end group
Console.WriteLine(match.Groups("end").Value)
End If
正则表达式的解释:
(?<start>REGEX)
=捕获名为start
(?<end>REGEX)
=捕获名为end
\d
=匹配数字
{X}
= X出现的匹配
.*
=确保我们匹配零或一个示例,这样两个组都不会命名为start
示例:
\d{4}
=匹配4位
Json方法是可能的,但我认为实现起来有点复杂,因为你的Json字符串中有一个非法名称:end
但是如果你想使用Json,你可以导入Newtonsoft.Json
有一个班级:
Public Class Rootobject
Public Property start As String
Public Property _end As String
End Class
然后像这样反序列化:
Dim obj = JsonConvert.DeserializeObject(Of Rootobject)(TextBox1.Text)
但是您需要实施:DataContract
和DataMember
处理单词end