vb.net正则表达式-添加"s"到页码 [英] vb.net regex - adding "s" to page numbers

查看:76
本文介绍了vb.net正则表达式-添加"s"到页码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我以为我已经接近完成了,但是两个小时后,我变得更加混乱,而不是取得进展.我需要在一些字符串中搜索"1页"或"34页"或"1页"或"1页"之类的部分,并进行修复.因此,如果字符串是这样的:

>>>文档1(第4页,共12页).

需要转换为此:

>>>文档1(第12页的第4页).


我有2种模式,正在进行2次替换.寻找"1"并检查"pages"是否为复数,然后将其修复为"1 page"即可.
但是,从单数变为复数会让我感到困惑.


I thought I was close on this but after 2 hours I''m making more of a mess of it than making progress. I need to search some strings for parts like "1 page" or "34 page" or "1 pages" or "1 pages" and fix them. So if a string is like this:

>>> Document 1 (p 4 of 12 page).

needs to be converted to this:

>>> Document 1 (page 4 of 12 pages).


I have 2 patterns and am doing 2 replaces. Looking for the "1" and checking to see if "pages" is plural and then fixing that so it reads "1 page" is working fine.
But changing from singular to plural is messing me up.


'Here's my code. Like I said, the first part is running just fine. It's the second part that's hanging me up.
Dim myString as string = "Document 1 (page 1 of 12 page)"
Dim rxPat As String
Dim rx As Regex
'first, is there only 1 page with "pages"; if so, take out the "s"
rxPat = "([^1-9][1]\s*?pages)"
rx = New Regex(rxPat, RegexOptions.Compiled Or RegexOptions.IgnoreCase)
If rx.IsMatch(myString) Then
    myString = rx.Replace(myString, rxPat, "1 page")
End If
'second, is there more than one page with just "page"; if so, add an "s"
rxPat = "(([1-9][01]|[2-9])\s*?)(page)([^s])"
rx = New Regex(rxPat, RegexOptions.Compiled Or RegexOptions.IgnoreCase)
If rx.IsMatch(myString) Then
    myString = rx.Replace(myString, rxPat, "$1$2s$3")
End If



还想提到上面的代码极大地减慢了我的进程.使用此代码的循环中,通常需要9秒钟左右的过程几乎翻了一番.我希望有一个更快的方法.我无法将处理时间加倍.那会杀了我.

希望有人能帮忙.

提前致谢. :-)

[edit]在第一行此处的单词前添加注释字符''-PES [/edit]



Also want to mention that the above code is slowing my process down immensely. A process that normally takes about 9 seconds is nearly doubled with this code in the loop. I''m hoping there''s a faster way. I can''t double the processing time. That''ll kill me.

hope someone can help.

Thanks in advance. :-)

[edit]Comment character '' added before Here word of the first line - PES [/edit]

推荐答案

1


2s


3") 结束 如果
3") End If



还想提到上面的代码极大地减慢了我的进程.使用此代码的循环中,通常需要9秒钟左右的过程几乎翻了一番.我希望有一个更快的方法.我无法将处理时间加倍.那会杀了我.

希望有人能帮忙.

提前致谢. :-)

[edit]在第一行此处的单词前添加注释字符''-PES [/edit]



Also want to mention that the above code is slowing my process down immensely. A process that normally takes about 9 seconds is nearly doubled with this code in the loop. I''m hoping there''s a faster way. I can''t double the processing time. That''ll kill me.

hope someone can help.

Thanks in advance. :-)

[edit]Comment character '' added before Here word of the first line - PES [/edit]


这篇关于vb.net正则表达式-添加"s"到页码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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