如何使用正则表达式或......做.... [英] How to using regular expression or ... for doing....?

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

问题描述

我有一个输入字符串,每个单词后跟两个标签,所以第一个单词名为label,第二个名称为标签,例如:

string inputStr =word1 / label1 / tag1 word2 / label2 / tag2 word3 / label3 / tag3 ...; // inputStr =this / l1 / t1 example / l2 / t2 is / l3 / t3 a / l4 / t4 test / l5 / t5



也有,我有许多字符串命名为规则,这条规则包含一些条件所以如果条件为真则删除输入字符串的一些连续字。

我想找到哪些规则可以应用于输入字符串。假设我们有一百个rulet,规则有以下结构:

ruleStr [i] =[MAX = 2] A: DEL(A,m),LB(A,side,lable),lOC(A,k,side,wtag);



DEL(A,m) :表示必须删除输入字符串的m个单词。这个m字被命名为中心段。

LB(A,side,lable):表示作为删除​​单词(中心段)左侧或右侧或中心位置的第一个单词的标签,侧面值为左侧,右侧或中央。

lOC(A,side,k,wtag):表示删除单词的左侧或右侧或中心(侧面)的k位置是单词(wtag)或标签(wtag)。 k变量是wtag的索引,因为中心段以one开头.wtag显示k位置的单词或标签。

如果wtag是标签,则在

通知:

*在每个ruleStr中,DEL条件的数量是一个,LB条件的最大数量是三(左,右和中),LB条件是*(> ; = 0)。

* DEL,LB,LOC条件在每个ruleStr中都是无序的。



请参阅以下示例:

string inputStr =A / l1 / t1 B / l2 / t2 C / l3 / t3 D / l4 / t4 E / l5 / t5 F / l6 / t6 G / l7 / t7 H / l1 / t1 ;

ruleStr [1] =[MAX = 2] A:DEL(A,2),LB(A,left,l2),lOC(A,2,right,F) ; // C,D被删除

ruleStr [2] =[MAX = 1] A:DEL(A,2),LB(A,左,l2),lOC(A,2) ,右,pos(t6)); // C,D被删除

ruleStr [3] =[MAX = 4] A:LB(A,left,l5),DEL(A ,1),LOC(A,2,左,d),LOC(A,1,右,pos(t7)); // F被删除

ruleStr [4] =[MAX = 2] A:DEL(A,1),LB(A,左,l1), lOC(A,3,right,C); // A被删除

ruleStr [5] =[MAX = 5] A:lOC(A,1,left,C),lOC (A,1,center,D),DEL(A,3),LB(A,right,l7),; // D,E,F被删除

ruleStr [6] = [MAX = 3] A:DEL(A,1),LB(A,center,l6),lOC(A,2,right,A); //不正确或不匹配输入字符串

ruleStr [7] =[MAX = 3] A:LB(A,左,l6),lOC(A,2,右,D),DEL(A,1); // NOT是或不匹配输入字符串



将每个ruleStr应用于inputStr后,我们有:

outStr1 =A / l1 / t1 B / l2 / t2 E / l5 / t5 F / l6 / t6 G / l7 / t7 H / l1 / t1;

outStr2 =A / l1 / t1 B / l2 / t2 E / l5 / t5 F / l6 / t6 G / l7 / t7 H / l1 / t1;

outStr3 =A / l1 / t1 B / l2 / t2 C / l3 / t3 D / l4 / t4 E / l5 / t5 G / l7 / t7 H / l1 / t1;

outStr4 =B / l2 / t2 C / l3 / t3 D / l4 / t4 E / l5 / t5 F / l6 / t6 G / l7 / t7 H / l1 / t1;

outStr5 =A / l1 / t1 B / l2 / t2 C / l3 / t3 G / l7 / t7 H / l1 / t1;

for ruleStr [6,7]条件不匹配....



如何找到可应用于输入字符串的规则?将每个输出字符串应用到输入字符串后输出字符串是什么?

任何人都可以在这方面帮助我吗?

解决方案

< blockquote class =quote>

Quote:

如何找到可应用于输入字符串的规则?将每个输出字符串应用到输入字符串后输出字符串是什么?

第一步是学习RegEx。

http://perldoc.perl.org/perlre.html [ ^ ]

https://www.debuggex.com/ [< a href =https://www.debuggex.com/target =_ blanktitle =新窗口> ^ ]


I have a input string that each word followed with two tags so the first tage of word named as label and second named as tag,for example:
string inputStr="word1/label1/tag1 word2/label2/tag2 word3/label3/tag3 ...";//inputStr="this/l1/t1 example/l2/t2 is/l3/t3 a/l4/t4 test/l5/t5"

also,I have many string named as rules that this rules incude some conditions so if that conditions are true then remove some of continuous words of input string .
I want to find which of rules can be applied to input string.Suppose that we have a hundred rulet,the rules have following structure:
ruleStr[i]="[MAX=2]A: DEL(A,m),LB(A,side,lable),lOC(A,k,side,wtag)";

DEL(A,m):means as m words of input string must be removed. this m words named as center segment.
LB(A,side,lable):means as the lable of first word in a position to left or right or center of the deleted words(center segment),the side value is left,right or center.
lOC(A,side,k,wtag):means as in k position to left or right or center(side) of deleted words is the word(wtag) or the tag(wtag).the k variable is index of wtag in terms as center segment starts with one.wtag shows a word or a tag in k position.
if wtag be a tag is shown as pos(tag) in
notice:
*In each ruleStr, number of DEL condition is one exactly and Maximum number of LB condition is three(left,right and center) and for LB condition is *(>=0).
*The DEL,LB,LOC conditions are disordered in each ruleStr.

see following example please:
string inputStr="A/l1/t1 B/l2/t2 C/l3/t3 D/l4/t4 E/l5/t5 F/l6/t6 G/l7/t7 H/l1/t1";
ruleStr[1]="[MAX=2]A: DEL(A,2),LB(A,left,l2),lOC(A,2,right,F)";// C,D is deleted
ruleStr[2]="[MAX=1]A: DEL(A,2),LB(A,left,l2),lOC(A,2,right,pos(t6))";// C,D are deleted
ruleStr[3]="[MAX=4]A: LB(A,left,l5),DEL(A,1),lOC(A,2,left,D),lOC(A,1,right,pos(t7))";// F is deleted
ruleStr[4]="[MAX=2]A: DEL(A,1),LB(A,left,l1),lOC(A,3,right,C)";// A is deleted
ruleStr[5]="[MAX=5]A: lOC(A,1,left,C),lOC(A,1,center,D),DEL(A,3),LB(A,right,l7),";// D,E,F are deleted
ruleStr[6]="[MAX=3]A: DEL(A,1),LB(A,center,l6),lOC(A,2,right,A)";// NOT TRUE OR DON'T MATCH FOR INPUT STRING
ruleStr[7]="[MAX=3]A: LB(A,left,l6),lOC(A,2,right,D),DEL(A,1)";// NOT TRUE OR DON'T MATCH FOR INPUT STRING

After apply each ruleStr to the inputStr we have:
outStr1="A/l1/t1 B/l2/t2 E/l5/t5 F/l6/t6 G/l7/t7 H/l1/t1";
outStr2="A/l1/t1 B/l2/t2 E/l5/t5 F/l6/t6 G/l7/t7 H/l1/t1";
outStr3="A/l1/t1 B/l2/t2 C/l3/t3 D/l4/t4 E/l5/t5 G/l7/t7 H/l1/t1";
outStr4="B/l2/t2 C/l3/t3 D/l4/t4 E/l5/t5 F/l6/t6 G/l7/t7 H/l1/t1";
outStr5="A/l1/t1 B/l2/t2 C/l3/t3 G/l7/t7 H/l1/t1";
for ruleStr[6,7] conditions do not match... .

How do I find the rules that can be applied to the input string? What is output string after applying each of them to the input string?
Anyone can help me in this respect,please?

解决方案

Quote:

How do I find the rules that can be applied to the input string? What is output string after applying each of them to the input string?

The first step is to learn RegEx.
http://perldoc.perl.org/perlre.html[^]
https://www.debuggex.com/[^]


这篇关于如何使用正则表达式或......做....的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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