Pattern.CASE_INSENSITIVE
I am working on a method that accepts a String argument parameter, searches a file for that word and returns a count of the occurrences of that word. I am using Java Regex Pattern and Matcher classes and methods. The way that I have implemented Pattern.CASE_INSENSITIVE does not seem to function correctly. It is still matching on a case sensitive basis.
public int lookup(String wrd) throws IOException, FileNotFoundException,
{
int cnt = 0;
BufferedReader in = new BufferedReader(new FileReader(this.filename));
String line = in.readLine();
while (line != null)
{
Pattern ptn = Pattern.compile(wrd, Pattern.CASE_INSENSITIVE);
Matcher mtch = ptn.mtch(line);
if (mtch.find()) {cnt++;}
line = input.readLine();
}
return cnt;
}
java regex
add a comment |
I am working on a method that accepts a String argument parameter, searches a file for that word and returns a count of the occurrences of that word. I am using Java Regex Pattern and Matcher classes and methods. The way that I have implemented Pattern.CASE_INSENSITIVE does not seem to function correctly. It is still matching on a case sensitive basis.
public int lookup(String wrd) throws IOException, FileNotFoundException,
{
int cnt = 0;
BufferedReader in = new BufferedReader(new FileReader(this.filename));
String line = in.readLine();
while (line != null)
{
Pattern ptn = Pattern.compile(wrd, Pattern.CASE_INSENSITIVE);
Matcher mtch = ptn.mtch(line);
if (mtch.find()) {cnt++;}
line = input.readLine();
}
return cnt;
}
java regex
2
Can you show us some sample input data? Both the pattern and a (small) part of the file that's being read from?
– mypetlion
Nov 21 '18 at 23:11
You have a bunch of weird mistakes in your code. What isinput
hereline = input.readLine();
. What is this methodptn.mtch(line);
,java.util.regex.Pattern
hasmatcher
method. If you want all occurrences of the word then you should dowhile (mtch.find())
instead ofif (mtch.find())
... Just fix it and probably it will work.
– Sergei Sirik
Nov 21 '18 at 23:28
add a comment |
I am working on a method that accepts a String argument parameter, searches a file for that word and returns a count of the occurrences of that word. I am using Java Regex Pattern and Matcher classes and methods. The way that I have implemented Pattern.CASE_INSENSITIVE does not seem to function correctly. It is still matching on a case sensitive basis.
public int lookup(String wrd) throws IOException, FileNotFoundException,
{
int cnt = 0;
BufferedReader in = new BufferedReader(new FileReader(this.filename));
String line = in.readLine();
while (line != null)
{
Pattern ptn = Pattern.compile(wrd, Pattern.CASE_INSENSITIVE);
Matcher mtch = ptn.mtch(line);
if (mtch.find()) {cnt++;}
line = input.readLine();
}
return cnt;
}
java regex
I am working on a method that accepts a String argument parameter, searches a file for that word and returns a count of the occurrences of that word. I am using Java Regex Pattern and Matcher classes and methods. The way that I have implemented Pattern.CASE_INSENSITIVE does not seem to function correctly. It is still matching on a case sensitive basis.
public int lookup(String wrd) throws IOException, FileNotFoundException,
{
int cnt = 0;
BufferedReader in = new BufferedReader(new FileReader(this.filename));
String line = in.readLine();
while (line != null)
{
Pattern ptn = Pattern.compile(wrd, Pattern.CASE_INSENSITIVE);
Matcher mtch = ptn.mtch(line);
if (mtch.find()) {cnt++;}
line = input.readLine();
}
return cnt;
}
java regex
java regex
edited Nov 21 '18 at 23:12
shmosel
35.8k43891
35.8k43891
asked Nov 21 '18 at 23:04
TacoB2018TacoB2018
183
183
2
Can you show us some sample input data? Both the pattern and a (small) part of the file that's being read from?
– mypetlion
Nov 21 '18 at 23:11
You have a bunch of weird mistakes in your code. What isinput
hereline = input.readLine();
. What is this methodptn.mtch(line);
,java.util.regex.Pattern
hasmatcher
method. If you want all occurrences of the word then you should dowhile (mtch.find())
instead ofif (mtch.find())
... Just fix it and probably it will work.
– Sergei Sirik
Nov 21 '18 at 23:28
add a comment |
2
Can you show us some sample input data? Both the pattern and a (small) part of the file that's being read from?
– mypetlion
Nov 21 '18 at 23:11
You have a bunch of weird mistakes in your code. What isinput
hereline = input.readLine();
. What is this methodptn.mtch(line);
,java.util.regex.Pattern
hasmatcher
method. If you want all occurrences of the word then you should dowhile (mtch.find())
instead ofif (mtch.find())
... Just fix it and probably it will work.
– Sergei Sirik
Nov 21 '18 at 23:28
2
2
Can you show us some sample input data? Both the pattern and a (small) part of the file that's being read from?
– mypetlion
Nov 21 '18 at 23:11
Can you show us some sample input data? Both the pattern and a (small) part of the file that's being read from?
– mypetlion
Nov 21 '18 at 23:11
You have a bunch of weird mistakes in your code. What is
input
here line = input.readLine();
. What is this method ptn.mtch(line);
, java.util.regex.Pattern
has matcher
method. If you want all occurrences of the word then you should do while (mtch.find())
instead of if (mtch.find())
... Just fix it and probably it will work.– Sergei Sirik
Nov 21 '18 at 23:28
You have a bunch of weird mistakes in your code. What is
input
here line = input.readLine();
. What is this method ptn.mtch(line);
, java.util.regex.Pattern
has matcher
method. If you want all occurrences of the word then you should do while (mtch.find())
instead of if (mtch.find())
... Just fix it and probably it will work.– Sergei Sirik
Nov 21 '18 at 23:28
add a comment |
2 Answers
2
active
oldest
votes
Just like I mentioned in the comment fix the mistakes. Here is a bit modified and fixed version of your code. This way seems like everything works:
public static int lookup(String wrd) throws IOException {
int cnt = 0;
BufferedReader in = new BufferedReader(
new StringReader(new String("word adfasdword avcasfn asdf WoRd asdfWORDasdf")));
String line = in.readLine();
while (line != null) {
Pattern ptn = Pattern.compile(wrd, Pattern.CASE_INSENSITIVE);
Matcher mtch = ptn.matcher(line);
while (mtch.find()) {
cnt++;
}
line = in.readLine();
}
return cnt;
}
public static void main(String args) throws IOException {
System.out.println(lookup("WORD"));
System.out.println(lookup("word"));
System.out.println(lookup("WorD"));
System.out.println(lookup("worLd"));
}
Output:
4
4
4
0
Thank you - the "While (mtch.find())" was the key issue in my code. I appreciate your help!
– TacoB2018
Nov 22 '18 at 11:09
add a comment |
You are not scrolling through the lines in the file and only reading first line. There is no reason to use regex, for most languages you can combine toLowerCase()
with indexOf()
to get a case insensitive position check.
public static void main(String args) throws Exception {
Path p = ...
String wrd = ...
System.out.println(totalMatches(p, wrd));
}
private static int totalMatches(Path path, String word) throws IOException {
try (BufferedReader reader = Files.newBufferedReader(path)) {
return reader.lines()
.mapToInt(l -> lineMatches(l, word))
.sum();
}
}
private static int lineMatches(String line, String word) {
int counter = 0, i = 0, found;
while ((found = line.toLowerCase().indexOf(word.toLowerCase(), i)) >= 0) {
counter++;
i = found + 1;
}
return counter;
}
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53421677%2fpattern-case-insensitive%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Just like I mentioned in the comment fix the mistakes. Here is a bit modified and fixed version of your code. This way seems like everything works:
public static int lookup(String wrd) throws IOException {
int cnt = 0;
BufferedReader in = new BufferedReader(
new StringReader(new String("word adfasdword avcasfn asdf WoRd asdfWORDasdf")));
String line = in.readLine();
while (line != null) {
Pattern ptn = Pattern.compile(wrd, Pattern.CASE_INSENSITIVE);
Matcher mtch = ptn.matcher(line);
while (mtch.find()) {
cnt++;
}
line = in.readLine();
}
return cnt;
}
public static void main(String args) throws IOException {
System.out.println(lookup("WORD"));
System.out.println(lookup("word"));
System.out.println(lookup("WorD"));
System.out.println(lookup("worLd"));
}
Output:
4
4
4
0
Thank you - the "While (mtch.find())" was the key issue in my code. I appreciate your help!
– TacoB2018
Nov 22 '18 at 11:09
add a comment |
Just like I mentioned in the comment fix the mistakes. Here is a bit modified and fixed version of your code. This way seems like everything works:
public static int lookup(String wrd) throws IOException {
int cnt = 0;
BufferedReader in = new BufferedReader(
new StringReader(new String("word adfasdword avcasfn asdf WoRd asdfWORDasdf")));
String line = in.readLine();
while (line != null) {
Pattern ptn = Pattern.compile(wrd, Pattern.CASE_INSENSITIVE);
Matcher mtch = ptn.matcher(line);
while (mtch.find()) {
cnt++;
}
line = in.readLine();
}
return cnt;
}
public static void main(String args) throws IOException {
System.out.println(lookup("WORD"));
System.out.println(lookup("word"));
System.out.println(lookup("WorD"));
System.out.println(lookup("worLd"));
}
Output:
4
4
4
0
Thank you - the "While (mtch.find())" was the key issue in my code. I appreciate your help!
– TacoB2018
Nov 22 '18 at 11:09
add a comment |
Just like I mentioned in the comment fix the mistakes. Here is a bit modified and fixed version of your code. This way seems like everything works:
public static int lookup(String wrd) throws IOException {
int cnt = 0;
BufferedReader in = new BufferedReader(
new StringReader(new String("word adfasdword avcasfn asdf WoRd asdfWORDasdf")));
String line = in.readLine();
while (line != null) {
Pattern ptn = Pattern.compile(wrd, Pattern.CASE_INSENSITIVE);
Matcher mtch = ptn.matcher(line);
while (mtch.find()) {
cnt++;
}
line = in.readLine();
}
return cnt;
}
public static void main(String args) throws IOException {
System.out.println(lookup("WORD"));
System.out.println(lookup("word"));
System.out.println(lookup("WorD"));
System.out.println(lookup("worLd"));
}
Output:
4
4
4
0
Just like I mentioned in the comment fix the mistakes. Here is a bit modified and fixed version of your code. This way seems like everything works:
public static int lookup(String wrd) throws IOException {
int cnt = 0;
BufferedReader in = new BufferedReader(
new StringReader(new String("word adfasdword avcasfn asdf WoRd asdfWORDasdf")));
String line = in.readLine();
while (line != null) {
Pattern ptn = Pattern.compile(wrd, Pattern.CASE_INSENSITIVE);
Matcher mtch = ptn.matcher(line);
while (mtch.find()) {
cnt++;
}
line = in.readLine();
}
return cnt;
}
public static void main(String args) throws IOException {
System.out.println(lookup("WORD"));
System.out.println(lookup("word"));
System.out.println(lookup("WorD"));
System.out.println(lookup("worLd"));
}
Output:
4
4
4
0
answered Nov 21 '18 at 23:32
Sergei SirikSergei Sirik
6591521
6591521
Thank you - the "While (mtch.find())" was the key issue in my code. I appreciate your help!
– TacoB2018
Nov 22 '18 at 11:09
add a comment |
Thank you - the "While (mtch.find())" was the key issue in my code. I appreciate your help!
– TacoB2018
Nov 22 '18 at 11:09
Thank you - the "While (mtch.find())" was the key issue in my code. I appreciate your help!
– TacoB2018
Nov 22 '18 at 11:09
Thank you - the "While (mtch.find())" was the key issue in my code. I appreciate your help!
– TacoB2018
Nov 22 '18 at 11:09
add a comment |
You are not scrolling through the lines in the file and only reading first line. There is no reason to use regex, for most languages you can combine toLowerCase()
with indexOf()
to get a case insensitive position check.
public static void main(String args) throws Exception {
Path p = ...
String wrd = ...
System.out.println(totalMatches(p, wrd));
}
private static int totalMatches(Path path, String word) throws IOException {
try (BufferedReader reader = Files.newBufferedReader(path)) {
return reader.lines()
.mapToInt(l -> lineMatches(l, word))
.sum();
}
}
private static int lineMatches(String line, String word) {
int counter = 0, i = 0, found;
while ((found = line.toLowerCase().indexOf(word.toLowerCase(), i)) >= 0) {
counter++;
i = found + 1;
}
return counter;
}
add a comment |
You are not scrolling through the lines in the file and only reading first line. There is no reason to use regex, for most languages you can combine toLowerCase()
with indexOf()
to get a case insensitive position check.
public static void main(String args) throws Exception {
Path p = ...
String wrd = ...
System.out.println(totalMatches(p, wrd));
}
private static int totalMatches(Path path, String word) throws IOException {
try (BufferedReader reader = Files.newBufferedReader(path)) {
return reader.lines()
.mapToInt(l -> lineMatches(l, word))
.sum();
}
}
private static int lineMatches(String line, String word) {
int counter = 0, i = 0, found;
while ((found = line.toLowerCase().indexOf(word.toLowerCase(), i)) >= 0) {
counter++;
i = found + 1;
}
return counter;
}
add a comment |
You are not scrolling through the lines in the file and only reading first line. There is no reason to use regex, for most languages you can combine toLowerCase()
with indexOf()
to get a case insensitive position check.
public static void main(String args) throws Exception {
Path p = ...
String wrd = ...
System.out.println(totalMatches(p, wrd));
}
private static int totalMatches(Path path, String word) throws IOException {
try (BufferedReader reader = Files.newBufferedReader(path)) {
return reader.lines()
.mapToInt(l -> lineMatches(l, word))
.sum();
}
}
private static int lineMatches(String line, String word) {
int counter = 0, i = 0, found;
while ((found = line.toLowerCase().indexOf(word.toLowerCase(), i)) >= 0) {
counter++;
i = found + 1;
}
return counter;
}
You are not scrolling through the lines in the file and only reading first line. There is no reason to use regex, for most languages you can combine toLowerCase()
with indexOf()
to get a case insensitive position check.
public static void main(String args) throws Exception {
Path p = ...
String wrd = ...
System.out.println(totalMatches(p, wrd));
}
private static int totalMatches(Path path, String word) throws IOException {
try (BufferedReader reader = Files.newBufferedReader(path)) {
return reader.lines()
.mapToInt(l -> lineMatches(l, word))
.sum();
}
}
private static int lineMatches(String line, String word) {
int counter = 0, i = 0, found;
while ((found = line.toLowerCase().indexOf(word.toLowerCase(), i)) >= 0) {
counter++;
i = found + 1;
}
return counter;
}
answered Nov 21 '18 at 23:24
Karol DowbeckiKarol Dowbecki
18.6k92850
18.6k92850
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53421677%2fpattern-case-insensitive%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
Can you show us some sample input data? Both the pattern and a (small) part of the file that's being read from?
– mypetlion
Nov 21 '18 at 23:11
You have a bunch of weird mistakes in your code. What is
input
hereline = input.readLine();
. What is this methodptn.mtch(line);
,java.util.regex.Pattern
hasmatcher
method. If you want all occurrences of the word then you should dowhile (mtch.find())
instead ofif (mtch.find())
... Just fix it and probably it will work.– Sergei Sirik
Nov 21 '18 at 23:28