java中查找字符索引,如何在java中查找字符串中的全字索引

I want to find out all starting indexes of whole word in a given string.

Lets say I have a string given below.

"an ancient manuscripts, another means to divide sentences into

paragraphs was a line break (newline) followed by an initial at the

beginning of the next paragraph. An initial is an oversize capital

letter, sometimes outdented beyond the margin of text. This style can

be seen, for example, in the original Old English manuscript of

Beowulf. Outdenting is still used in English typography, though not

commonly.[4] Modern English typography usually indicates a new

paragraph by indenting the first line"); "

I would like to find out the starting index of "paragraph" only. Which should not include "paragraphs", "paragraph.".

Can anyone give an idea how to do it in java.

Thanks in advance.

解决方案

You can use a regexp with word boundaries character:

String text = "an ancient manuscripts, another means to divide sentences into paragraphs was a line break (newline) followed by an initial at the beginning of the next paragraph. An initial is an oversize capital letter, sometimes outdented beyond the margin of text. This style can be seen, for example, in the original Old English manuscript of Beowulf. Outdenting is still used in English typography, though not commonly.[4] Modern English typography usually indicates a new paragraph by indenting the first line";

Matcher m = Pattern.compile("\\bparagraph\\b").matcher(text);

while (m.find()) {

System.out.println("Matching at: " + m.start());

}

If you don't want "paragraph." ("paragraph" followed by a dot), you can try

Matcher m = Pattern.compile("\\bparagraph($| )").matcher(text);

which means paragraph followed by a space or a end-of-line.

If the String you are looking for can include special characters (like "("), you can use Pattern.quote() to escape it:

String mySearchString = "paragraph";

Matcher m = Pattern.compile("\\b" + Pattern.quote(mySearchString) + "($| )").matcher(text);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值