繁体   English   中英

Java模式匹配器分配

[英]Java pattern matcher assignment

我需要创建一个程序,其中我用户模式匹配器执行以下操作。 我知道这需要创建一个正则表达式。 请帮我创建一个模式: - 如果我有一个段落,我需要做以下事情:


a) if the number of letters in a word is greater than 3, then it should be changed to first three characters + $. For example, the word "Maradona" should be changed to "Mar$". If the number of letters is less than or equal to 3, leave it as it is. Numbers are not counted as letters. So
    "$16.9m." should not be changed.

b)Punctucation should be intact(Except if it is in the word. ie "tournament's" should be "tou$"). - Eg: history, -> his$,

c) "/n/n" is for line termination. It shouldn't be changed.

适用于: -

马拉多纳是唯一一位两次创造世界纪录合同费用的足球运动员,首先是当时转会到巴塞罗那以创造500万英镑的世界纪录,其次,当转移到那不勒斯再获得1690万美元的创纪录费用时。 在他的职业俱乐部生涯中,马拉多纳效力于阿根廷青年队,博卡青年队,巴塞罗那队,那不勒斯队,塞维利亚队和纽维尔队的老男孩队。 在俱乐部层面,他最出名的是他在那不勒斯的职业生涯,并赢得了无数赞誉。 在他的国际生涯中,他为阿根廷队效力,他赢得了91个球,并打入了34个进球./n/n他参加了四次FIFA世界杯比赛,包括1986年的比赛,在那里他带领阿根廷队并带领他们战胜西德队。决赛中,赢得金球奖作为锦标赛最佳球员。 在同一场比赛的四分之一决赛中,他以2比1击败英格兰进入足球历史,但两个原因不同。 第一个目标是通过一个称为“上帝之手/”的无人手球,而第二个目标是在经过五个英格兰球员的60米(66码)后运球./n/n/"The Century of the Century /“ 2002年被FIFA.com选民授予马拉多纳。

输出应该是: -

Mar $是$ foo $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $对于ano $ rec $费用1690万美元。 Dur $他的pro $ clu $ car $ Mar $ pla $为Arg $ Jun $,Boc $ Jun $,Bar $,Nap $,Sev $和New $ Old Boy $。 在clu $ lev $,他的汽车价格为$ $ $ $ $ $ $ $ $ $ $ $ $ 在他的$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,当他花费$ Arg $并将$ $带到$ vic $ $ $ Wes $ Ger $时,赢得$ Gol $ Bal $ awa $作为$ $ $ $ $ $ $。 在$ $ s $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ 冷杉$ goa $通过$ $ han $ kno $作为/“Han $ of God /”,秒$ $ goa $ fol $ 60 m(66码)dri $ pas $ fiv $ Eng $ pla $。/ n / n /“Cen $的Goa $ /”在2002年的FIF $ vot $中以3美元的价格卖出。

编辑这是我到目前为止所尝试的:

我试图通过不使用模式编译方法来做到这一点。 只需使用以下条件:

String[] split = sentence.split("\\s+");
for(int i = 0; i < split.length; i++)
{
    if(split[i].length() > 3)
    {
        if(split[i].matches("[a-zA-Z]+"))
    }
}

但这似乎不是一种有效的方法。

这个replaceAll应该工作:

String repl = data.replaceAll("(?<=\\b[a-zA-Z']{3})[\\w']+", "\\$");

说明:此正则表达式找到一个或多个单词字符,前面是“单词边界和3个字母”。 一旦找到,我们用文字$替换这个文本。

搜索:

(?<=\b[a-zA-Z']{3}) Positive Lookbehind - Assert that the regex below can be matched
\b assert position at a word boundary
[a-zA-Z']{3} match a single character present, Exactly 3 times
a-z a single character in the range between a and z
A-Z a single character in the range between A and Z
' is literal single quote
[\w']+ matches any word character or single quote [`a-zA-Z0-9_] one or more time

替换:

\\$ - A literal $

试试这个:

str = str.replaceAll("(?i)(?<=\\s[a-z']{3})[a-z']+", "\\$");

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM