Having find() find more than once in regex in java - java

I need find() to find more than once. For example, in the regex below it will only get "i am cool1", but I also want it to get "i am cool2" and "i am cool3". How would I do that?
Pattern pattern = Pattern.compile("i am cool([0-9]{1})", Pattern.CASE_INSENSITIVE);
String theString = "i am cool1 text i am cool2 text i am cool3 text";
Matcher matcher = pattern.matcher(theString);
matcher.find();
whatYouNeed = matcher.group(1);

You have to invoke find() for every match. You can get the whole match with group() (without index).
Pattern pattern = Pattern.compile("i am cool([0-9]{1})", Pattern.CASE_INSENSITIVE);
String theString = "i am cool1 text i am cool2 text i am cool3 text";
Matcher matcher = pattern.matcher(theString);
while (matcher.find()) {
System.out.println(matcher.group());
}
This will print
i am cool1
i am cool2
i am cool3

Related

Using regex in java to get a word from a string [duplicate]

This question already has answers here:
Difference between matches() and find() in Java Regex
(5 answers)
Closed 3 years ago.
I need to find the word "best" in a string using regex but it's throwing a "no match found" error. What am I doing wrong?
Pattern pattern = Pattern.compile("(best)");
String theString = "the best of";
Matcher matcher = pattern.matcher(theString);
matcher.matches();
String whatYouNeed = matcher.group(1);
Log.d(String.valueOf(LOG), whatYouNeed);
As per your requirement you have to find the string "best" in "the best of", so find() method suits your requirement instead of matches(). Please find the sample code snippet below:
Pattern pattern = Pattern.compile("best");
String theString = "the best of";
Matcher matcher = pattern.matcher(theString);
if(matcher.find()) {
System.out.println("found");
}else {
System.out.println("not found");
}
}
Use find() not matches!
public static void main(String[] args){
Pattern pattern = Pattern.compile("(best)");
String theString = "the best of";
Matcher matcher = pattern.matcher(theString);
if(matcher.find())
System.out.println("Hi!");
}
What I think you want is this.
String theString = "the best of";
String regex = "(best)";
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(theString);
while (m.find()) {
String result = m.group(1);
System.out.println("found: " + result);
}
outputs:
found: best

Java Pattern regex whole word match

I am trying to match a keyword with following string
"abc,pqr(1),xyz"
It will be succesfull match if the whole one word matched for e.g. "par" or "abc" or "xyz"
Can anyone please help me in creating regex for this match ?
String text = "hello, hellos(1),bye";
String keyword = "account";
String patternString = "["+ keyword + "]";
Pattern pattern = Pattern.compile(patternString, Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(text);
boolean matches = matcher.matches();
System.out.println("matches = " + matches);
This Should Work.
([a-zA-Z]+)
Input:
"abc,pqr(1),xyz"
Output:
abc
pqr
xyz
See: https://regex101.com/r/Us6G3X/2

use java regex,how can i get group value with "( )" string?

I have strings:
#Table(name = "T_MEM_MEMBER_ADDRESS1")
#Table( name = "T_MEM_MEMBER_ADDRESS2")
#Table ( name = "T_MEM_MEMBER_ADDRESS3" )
I want to write a regex, which can get the name value,such as :
T_MEM_MEMBER_ADDRESS1
T_MEM_MEMBER_ADDRESS2
T_MEM_MEMBER_ADDRESS3
I write
String regexPattern="...";
Pattern pattern = Pattern.compile(regexPattern);
Matcher matcher = pattern.matcher(input);
boolean matches = matcher.matches();
if (matches){
log.debug(matcher.group(1));
}
but i cannot write the regexPattern..
You can use this regex:
(?<=")(.+)(?=")
In Java:
String regexPattern="(?<=\")(.+)(?=\")";
It uses look-behinds and lookaheads.
Group 1 will contain what you want.
You can use this piece of code:
String input = "#Table(name = \"T_MEM_MEMBER_ADDRESS1\")";
String regexPattern=".*\"(.*)\".*";
Pattern pattern = Pattern.compile(regexPattern);
Matcher matcher = pattern.matcher(input);
boolean matches = matcher.matches();
if (matches){
System.out.println(matcher.group(1));
}
Hope it helps.

how to deal with a string with regex

for example:
I have a string like this:
http://shop.vipshop.com/detail-97996-12358781.html
I want to use regex to find 97996 and 12358781
java code is appreciated
Many thanks.
String str="http://shop.vipshop.com/detail-97996-12358781.html";
String regex ="\-d{5}\-";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(str);
System.out.println(matcher.group());
but it was wrong
Try this
String str="http://shop.vipshop.com/detail-97996-12358781.html";
String regex =".*detail-(\\d+)-(\\d+).html";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(str);
if(matcher.matches()){
System.out.println(matcher.group(1) + "|" + matcher.group(2));
}
You have to invoke either Matcher#find() or Matcher#matches() to actually get the matches. In this case, you would need the former one, as you are only finding a part of string matching the regex.
And you can use + quantifier to get any length of digit. Try using this:
String regex ="\\d+";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(str);
while (matcher.find()) {
System.out.println(matcher.group());
}
Two lines:
String num1 = str.replaceAll(".*-(\\d+)-.*", "$1");
String num2 = str.replaceAll(".*-(\\d+)\\..*", "$1");
String str = "http://shop.vipshop.com/detail-97996-12358781.html";
String regex = "(?<=detail-)(\\d+)-(\\d+)(?=\\.html)";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(str);
matcher.find();
System.out.println(matcher.group(1));
System.out.println(matcher.group(2));
output:
97996 12358781
You have used a quantifier (5) and still the 1235... is 8 characters long?
Is it always 5 and 8 can you use:
"([\\d]{5,8})"
The matches captured into backreferences
But if you need to find in the specific form detail-NUMBER-NUMBER.html you can use:
"detail-([\\d]*)-([\\d]*).html"
The matches captured in [1] and [2]
you can use this:
String str="http://shop.vipshop.com/detail-97996-12358781.html";
String regex ="[^0-9]+([0-9]+)[^0-9]+([0-9]+).+";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(str);
if(matcher.matches()){
System.out.println(matcher.group(1) + " " + matcher.group(2));
}
for advance tutorial go to this link
RegEx tutorial
and Regular Expression tutorial
You should add
if(matcher.find()){
}
on
System.out.println(matcher.group());
then your code is:
String str="http://shop.vipshop.com/detail-97996-12358781.html";
String regex ="\\d{5,}";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(str);
if(matcher.find()){
System.out.println(matcher.group());
}

Regx for extracting substring from in between data using java

String line = "asdasdasdasd <meta name=\"generator\" content=\"WordPress 3.5.2\" /> asdasdasdasdasd";
Pattern p = Pattern.compile("<meta name=\"generator\" content=\"WordPress\\s+([\\d.]+)\" />");
Matcher m = p.matcher(line);
if(m.matches())
System.out.println(m.group(1));
else
System.out.println("not found");
The regex I have used does not give the desired result. I want the wordpress version from the supplied string.
Matcher#matches() matches at the beginning of the string. So, you would need to build regex for complete string.
Alternatively, you can use Matcher#find() with just the regex for relevant part of the string:
Pattern p = Pattern.compile("content=\"WordPress\\s+([\\d.]+)\"");
Matcher m = p.matcher(line);
if(m.find())
System.out.println(m.group(1));
else
System.out.println("not found");
You have to escape the dot and accept more numbers just in case
Pattern p = Pattern.compile("WordPress\\s+([\\d+\\.]+)");

Categories