如何使用正则表达式检查票号是否格式化? [英] How can I use regex to check if ticket number is formatted?

查看:106
本文介绍了如何使用正则表达式检查票号是否格式化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望我的功能遵循一些惯例来检查我的票号是否需要格式化。

如果不符合约定,那么我想对票号进行一些更改。

示例

票号 19K3072216 需要格式化为此 19-K3-07-002216 ,因为它不符合以下条件。

For这个票号,我想做以下事项:

1.检查前2位数是否有0 - 9(数字)

2.检查是否第3位数字的值为A或Z

3.检查第4位数字的值是否为0 - 9(数字)

4.检查第5位和第6位数字有一个日期值(例如2位数年--17,90,15等)

5.检查下6位数字,即第7位到第12位数字是否为数字

因为票数字 19K3072216 不符合上述条件,我希望我的功能将其格式化为这样 19-K3-07-002216

因此,返回strCaseNumber应包含格式化的票号 19-K3-07-002216

我的vb.n et function

I would like my function to follow some convention to check if my ticket number needs formatting or not.
If the convention is not met, then I would like to make some changes to the ticket number.
Example
Ticket number 19K3072216 needs to be formatted to this 19-K3-07-002216 because it does not meet the following conditions.
For this ticket number, I would like to do the following:
1. Check if the 1st 2 digits has a value 0 - 9 (numeric)
2. Check if the 3rd digit has a value of A or Z
3. Check if the 4th digit has a value 0 - 9 (numeric)
4. Check if the 5th and 6th digits has a date value (e.g.2 digit year - 17, 90, 15 etc)
5. Check if the next 6 digits i.e. 7th - 12th digits are numeric
Because ticket number 19K3072216 does not meet the above conditions, I would like my function to format it to look like this 19-K3-07-002216
So Return strCaseNumber should contain formatted ticket number 19-K3-07-002216
My vb.net function

Public Class Ticket_Code

    Public Shared Sub main()
        Dim strTicketNumber As String = FixTicketNumber("19K3072216")

    End Sub
    
    Public Shared Function FixCaseNumber(ByVal astrCaseNumber As String) As String
        Dim strCaseNumber As String = Replace(astrCaseNumber, "-", "")

        'Determine if ticket number is formatted
         How do I do this?


        'If ticket number is formatted add 2 zeros
         'How do I do this?

        'Else return unchanged
         'If ticket number is already formatted, just returned the number (original number)

        Return strCaseNumber

    End Function


End Class

What I have tried:

<pre lang="vb">Public Shared Function FixCaseNumber(ByVal astrCaseNumber As String) As String
        Dim strCaseNumber As String = Replace(astrCaseNumber, "-", "")

        'Determine if case is a converted TCIS case number



        'If TCIS converted case number add zero if necessary


        'Else return unchanged

        Return strCaseNumber

    End Function

推荐答案

我会使用RegEx,首先检查值是否符合预期,格式化与否,然后我会进行替换以强制格式化。

我会从类似(\\\)的RegEx开始 - ?([AZ] \d) - ?

-----

以下是RegEx文档的链接:

perlre - perldoc.perl.org [ ^ ]

这里是帮助构建RegEx并调试它们的工具的链接:

.NET正则表达式测试程序 - 正则表达式风暴 [ ^ ]

快速正则表达式工具 [ ^ ]

这个显示RegEx是一个很好的图表,这对于理解什么是非常有用的执行RegEx:

Debuggex:在线可视正则表达式测试程序。 JavaScript,Python和PCRE。 [ ^ ]
I would use RegEx , first to check if value match expectations, formatted or not, then I would do a replace to force formatting.
I would start with a RegExlike like (\d\d)-?([A-Z]\d)-? .
-----
Here is a link to RegEx documentation:
perlre - perldoc.perl.org[^]
Here is links to tools to help build RegEx and debug them:
.NET Regex Tester - Regex Storm[^]
Expresso Regular Expression Tool[^]
This one show you the RegEx as a nice graph which is really helpful to understand what is doing a RegEx:
Debuggex: Online visual regex tester. JavaScript, Python, and PCRE.[^]


这篇关于如何使用正则表达式检查票号是否格式化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆