

Regex stops at first occurrence in java
source link: https://www.codesd.com/item/regex-stops-at-first-occurrence-in-java.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Regex stops at first occurrence in java
I think there is a problem with my regex because when I looked for resolve my problem, I found only question about wanted to stop at the first occurrence. Although, I want my regex to go after the first occurrence.
Here is my code:
String test = "\\d";
String word = "a1ap1k7";
Pattern p = Pattern.compile(test);
Matcher b = p.matcher(word);
if (b.find()) {
int start = b.start();
String text = b.group();
int stop = b.end();
System.out.println(start + text + stop);
}
It gives me just: 1(for the start)1(for the text)2(for the end)
You made just a simple error in writing this. You have to keep calling find
public static void main(String[] args){
String test = "\\d";
String word = "a1ap1k7";
Pattern p = Pattern.compile(test);
Matcher b = p.matcher(word);
while (b.find())
{
int start = b.start();
String text = b.group();
int stop = b.end();
System.out.println(start + text + stop);
}
}
Recommend
-
12
Co-occurrence of Pokemon Types (Gen 1-6) with Chord DiagramsJoin the Patreon to get Exclusive Downloads, Direct Support, Early Access, Voting Access, our Books...
-
4
Occurrence and Co-occurrence of Pokemon Types with Chord DiagramsJoin the Patreon to get Exclusive Downloads, Direct Support, Early Access, Voting Access, our Books...
-
5
Co-occurrence of Pokemon Types (Gen 1-8) with Chord DiagramsJoin the Patreon to get Exclusive Downloads, Direct Support, Early Access, Voting Access, our Books...
-
11
Co-occurrence of Movie Genres with Chord DiagramsJoin the Patreon to get Exclusive Downloads, Direct Support, Early Access, Voting Access, our Books for...
-
19
Co-occurrence of Anime Genres with Chord DiagramsJoin the Patreon to get Exclusive Downloads, Direct Support, Early Access, Voting Access, our Books for...
-
13
Animal Crossing VillagersJoin the Patreon to get Exclusive Downloads, Direct Support, Early Access, Voting Access, our Books for Free
-
4
Random numbers of an array in another array with different rules of occurrence in java advertisements I need help for an exercice in school.
-
8
Replace first occurrence of a substring in Python In this article, we will discuss different ways to replace the first occurrence of a substring from a string in Python. Suppose we have a string,
-
7
Calcifer Howl 9 hours ago Find First Occurrence ']' 30 Views
-
6
Delete first occurrence of given Key from a Linked ListGiven a linked list and a key to be deleted. The task is to delete the first occurrence of the given key from the linked list. Note: The...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK