1

I am searching this string package com.

This is the code

Pattern l_pattern = Pattern.compile("package com\\.",Pattern.CASE_INSENSITIVE);
String l_entireFile=readEntireFile(p_filePath.toString());
String l_spiltCommentString []  =   l_pattern.split(l_entireFile);

But it is not searching the same in all the file.

I just want to know why it is showing this behavior.

1
  • it was not searching the contents because we have tab and double spaces in the package name.!!!Thanks Commented Aug 6, 2013 at 9:51

2 Answers 2

3

Just use .indexOf():

l_entireFile.indexOf("package com.") != -1

Also, if this is really a Java source file, there is no need for Pattern.CASE_INSENSITIVE: both keywords and package names are case sensitive in Java.

Sign up to request clarification or add additional context in comments.

3 Comments

indexOf() won't work because we don't know which/how many whitespace characters to expect, so you would have to use Matcher.find() instead. But split() is definitely the wrong tool for this job.
@AlanMoore "[...]we don't know which/how many whitespace characters to expect," <-- sorry, but I see no relationship at all between whitespaeces and the text to search; unless you mean that there can be an arbitrary number of spaces between package and .com?
Yes, that's what I meant. And according to the OP's comment, that was indeed the problem.
2

Escape backslashes.

Pattern l_pattern = Pattern.compile("package com\\\\.",Pattern.CASE_INSENSITIVE);

UPDATE

You don't need to use regular expression.

String l_spiltCommentString []  = l_entireFile.split("package com.");

Package name contains spaces

Use following pattern.

Pattern l_pattern = Pattern.compile("package\\s+com\\.",Pattern.CASE_INSENSITIVE);

2 Comments

I am putting \\ to escape '.'.
Thanks actually it was not searching the contents because we have tab and double spaces in the package name.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.