如何在单词

时间:2017-05-10 11:09:13

标签: java regex

正则表达式的新功能,并使用以下代码查找单词在结尾/开头是否包含特殊字符。

String s = "K-factor:";
        String regExp = "^[^<>{}\"/|;:.,~!?@#$%^=&*\\]\\\\()\\[0-9_+]*$";
        Matcher matcher = Pattern.compile(regExp).matcher(s);
        while (matcher.find()) {
            System.out.println("Start: "+ matcher.start());
            System.out.println("End: "+ matcher.end());
            System.out.println("Group: "+ matcher.group());
            s = s.substring(0, matcher.start());
        }

想要查找字符串开头或结尾是否有任何特殊字符(在此示例代码中)。试图跳过这个角色。 既没有编译时错误也没有输出。

1 个答案:

答案 0 :(得分:1)

请注意,您的正则表达式匹配整个字符串,该字符串不包含您在字符类中定义的字符。有问题的字符串与该模式不匹配,因为它包含 <div class="container"> <div class="row"> <div class="col-lg-3 col-xs-6 Imgdiv"> <img src="Images/Car.jpg" class="img-responsive Imagesize" /> <p class="Imagecontent">A. P. De Zoysa was born in Randombe, Ambalangoda in the Southern province of Ceylon (now Sri Lanka). His parents died in an epidemic when he was eleven, and thereafter he was brought up by his grandmother. He started his education in the nearby historic temple, the Maha Samudraramaya, and then attended the Wesleyan school in Randombe. He continued his secondary education at Mahinda College, Galle, where he came under the influence of its principal, the famous Theosophist and Pali scholar Frank Lee Woodward.[1] Later he moved to Wesley College in Colombo.</p> </div> <div class="col-lg-3 col-xs-6 Imgdiv "> <img src="Images/Rose.jpg" class="img-responsive Imagesize" /> <p class="Imagecontent">Then he taught for a few years at Ananda College, Colombo and at Royal College Colombo. In 1921 he went to England and continued his higher education. In London he supported himself by coaching overseas students, and his wide social circle included the artist William Roberts, who painted his portrait. After taking an external London degree, in 1927 he was called to the Bar at Gray's Inn, and in 1929 he obtained a PhD in anthropology at London University for a dissertation on 'Observances and Customs in Sinhalese Villages'.[2]</p> </div> <div class="col-lg-3 col-xs-6 Imgdiv"> <img src="Images/Bike.jpg" class="img-responsive Imagesize" /> <p class="Imagecontent">With his wife, née Eleanor Hutton, whom he had met at the Buddhist mission in London and married in 1929, in 1934 de Zoysa returned to Sri Lanka, and began work as a lawyer. He was elected to represent Colombo South in 1936, and he continued to serve in the State Council (as an independent) until 1947. Causes which he supported included opposition to the death penalty, anti-dowry legislation, and improved state education. For many years he was also a municipal councillor in Colombo, taking up local issues and campaigning to improve the city's amenities.</p> </div> <div class="col-lg-3 col-xs-6 Imgdiv"> <img src="Images/Strawberry.jpg" class="img-responsive Imagesize" /> <p class="Imagecontent">In 1939 de Zoysa bought a printing press, and began to produce a series of educational books in Sinhala; he also edited a weekly paper, the Dharmasamaya. But his greatest project, which took over twenty years, with help from Buddhist scholars, was to publish a translation of the whole Tripitaka canon of Buddhist scripture into simple Sinhala; this eventually ran to forty-eight volumes. A concise edition, in about ten volumes, was incomplete at his death. He also compiled and printed English–Sinhala and Sinhala-English dictionaries</p> </div> </div> </div>

您可以考虑将模式拆分为两部分,以使用交替组检查开始或结束处的不需要的字符:

:

此处,模式具有String regExp = "^[<>{}\"/|;:.,~!?@#$%^=&*\\]\\\\()\\[0-9_+]|[<>{}\"/|;:.,~!?@#$%^=&*\\]\\\\()\\[0-9_+]$"; 结构,^<special_char_class>|<special_char_class>$在开始时锚定匹配,^在字符串结尾处锚定匹配,$是交替运算符。注意我从字符类的开头删除了|以使它们为正而不是否定,以便它们可以匹配在那里定义的字符/范围类。

或者,因为如果字符串在开头/结尾包含非字母,您似乎只匹配字符串,您可以使用

^

是Unicode字母识别或 - 仅ASCII:

String regExp = "^\\P{L}|\\P{L}$";