需要正则表达式才能找到模式的最后一次出现 [英] Need regex to find last occurance of the pattern

查看:129
本文介绍了需要正则表达式才能找到模式的最后一次出现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我有一个包含一些内容的文件。它将有一些文本行,然后是key = value字符串。下面给出一个例子。

SomeFile.txt

============
有些文字在这里


..结果,.. ..搜索结果===========,点击允许= text1.bat,点击允许= text2.bat,点击允许= text3.bat <无线电通信>结果。结果,=======结果,拒绝= deny1.bat结果,拒绝= deny2.bat结果,..结果。搜索结果=== =
someaction = action1.bin
someaction = action2.bin。



有时我们需要将新操作添加到此文件中,例如allow = text4.bat或deny = deny3.bat

所以我需要创建一个正则表达式来识别每个动作的最后一次出现,如("允许/拒绝/某些动作"),并在文件下面插入新动作并再次保存。所以它应该添加"allow = text4.bat"在允许文件中的部分和"deny = deny3.bat"结束时在拒绝文件的一部分。

输出和保存的文件应该是这样的

SomeFile.txt



============

一些文字在这里


..

..

..

..

===========

allow = text1.bat

allow = text2.bat

allow = text3。 bat
allow = text4.bat





=======

deny = deny1.bat

deny = deny2.bat
allow = deny3.bat

..





====

someaction = action1.bin

someaction = action2.bin。



=========
任何人都可以帮忙在这。我在网上搜索了很多,但我需要做的没有任何indexOf或lastindexOf概念。我们可以使用单个正则表达式模式识别最后一次出现吗?



---谢谢&问候,Guhan

解决方案

嗨Guhan,
这个模式寻找你的关键词allow,deny和somaction,并找到每个出现的并不是紧跟着a形成关键字。然后根据您要添加的keyword = value的类型进行替换。假设是1。您的关键字已分组,
2。在行的末尾没有额外的字符,
3。关键字前面没有字符,
4。那里没有额外的行,空白或其他类似的关键词之间
5。在关键字组之间有一些额外的某些行(最后一组不需要)

如果所有这些都是真的,那么这个代码应该有效...




< colgroup>






































< tr>































Hi everybody,

I have a file with some contents in it. It will have some text lines then followed by key = value strings. An example is given below.

SomeFile.txt

============
some text goes here
..
..
..
..
===========
allow=text1.bat
allow=text2.bat
allow=text3.bat
.
.
=======
deny=deny1.bat
deny=deny2.bat
..
.

====
someaction=action1.bin
someaction=action2.bin.
.

Some time we get new actions need to be added to this file like allow=text4.bat or deny=deny3.bat

So I need to create a regular expression which identifies the last occurance of each actions like ("allow/deny/someaction") and insert new actions beneath them in the file and save it again.so it should add "allow=text4.bat" at the end of allow part in the file and "deny=deny3.bat"  in deny part of the file.

The output and saved file should be like this

SomeFile.txt

============
some text goes here
..
..
..
..
===========
allow=text1.bat
allow=text2.bat
allow=text3.bat
allow=text4.bat
.
.
=======
deny=deny1.bat
deny=deny2.bat
allow=deny3.bat
..
.

====
someaction=action1.bin
someaction=action2.bin.
.

=========
Can anyone help in this. I searhed a lot over the net but I need to do without any indexOf or lastindexOf concepts. Can we identify the last occurance using a single regex pattern?



--- Thanks & Regards, Guhan

解决方案

Hi Guhan,
This pattern looks for your keywords allow, deny and somaction and finds each occurance that is not immediately followed with the a similarly formed keyword.  Then it makes a replacement based upon the kind of keyword=value you are adding.  The assumptions are
1.  your keywords are grouped,
2.  there are no extra characters at the end of the line,
3.  there are no characters preceding the keyword,
4.  there there are no extra lines, blank or otherwise between like keywords
5.  there IS an extra line of somekind between groups of keywords (not required for the last group)

If all these are true, then this code should work...

string pattern = @ "(allow | deny | someacti on)= [^ \\\\ n] * \\\\ n(?!(允许|拒绝|某些行动)= [^ \\ n] *)" ;
string test = @ "
============
有些文字在这里
..
..
..
..
===========
allow = text1.bat
allow = text2.bat
allow = text3.bat
=======
deny = deny1.bat
deny = deny2.bat
..
====
someaction = action1.bin
someaction = action2.bin。
" ;
Regex rx = new 正则表达式(模式);
string [] tests = {
" allow = text4.bat"
" deny = deny3.bat"
&some; someaction = action3.bin"
};
foreach string tst tests)
Console.WriteLine( " {0}" ,rx.Replace (试验,(M)=>(m.Groups [1]。价值== tst.Substring(0,tst.IndexOf( '=')))+ m.Value TST:m.Value));
            string pattern = @"(allow|deny|someaction)=[^\r\n]*\r\n(?!(allow|deny|someaction)=[^\r\n]*)";  
            string test = @"
============
some text goes here
..
..
..
..
===========
allow=text1.bat
allow=text2.bat
allow=text3.bat
.
.
=======
deny=deny1.bat
deny=deny2.bat
..
.
====
someaction=action1.bin
someaction=action2.bin.
.
";  
            Regex rx = new Regex(pattern);  
            string[] tests = {  
                                 "allow=text4.bat",  
                                 "deny=deny3.bat",  
                                 "someaction=action3.bin",  
                             };  
            foreach (string tst in tests)  
                Console.WriteLine("{0}", rx.Replace(test, (m) => (m.Groups[1].Value == tst.Substring(0,tst.IndexOf('='))) ? m.Value + tst : m.Value));  
 


这篇关于需要正则表达式才能找到模式的最后一次出现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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