I'm new to using Regex
I want to get macapp value in url just get number 12 to String how do that ?
String url = "stackoverflow.com/questions/ask:macapp=12";
Pattern pattern = ;// ?
Matcher matcher =;// ?
if(matcher.find())
{
//result = only 12
}
THX for your TIME
Pattern p = Pattern.compile("^.*:macapp=(\\d+)$");
Matcher m = p.matcher(s);
if (m.find()) {
int n = Integer.valueOf(m.group(1));
...
}
Related
" 294618 is your One Time Passcode (OTP) for the request "
How to extract only the numbers from the String given above?
Following function will return you the first integer that is part of string.
public static String getOtp(String string) {
String sPattern = "[^0-9]*([0-9]+).*";
Pattern pattern = Pattern.compile(sPattern);
Matcher matcher = pattern.matcher(string);
while (matcher.find()) {
String s = matcher.group(1);
return s;
}
return null;
}
This will print out all numbers from your String:
Pattern p = Pattern.compile("\\d+");
Matcher m = p.matcher(" 294618 is your One Time Passcode (OTP) for the request ");
while(m.find()) {
System.out.println(m.group());
}
String yourString = "294618 is your One Time Passcode (OTP) for the request";
String extractNo= yourString.replaceAll("[^0-9]", "");
System.out.println(extractNo);
I want to split my String (e.g. "20150101") using Regular Expression.
For example I need these values: "2015","01","01"
String pattern = "(....)(..)(..)";
Pattern r = Pattern.compile(pattern);
Matcher m = r.matcher(inputString);//inputString:"20150101"
Now you can use m.group(x) to get the parts of the string. For example:
m.group(1) is first four digit ("2015" in your question).
Bit hard to say without more details, but try:
(\d{4})(\d{2})(\d{2})
Your Matcher's three captured group references will then have the values you want.
Just combining the two answers both are valid
First
public static void main(String[] args) {
String input = "20150101";
String pattern = "(....)(..)(..)";
Pattern r = Pattern.compile(pattern);
Matcher m = r.matcher(input);
m.find();
for(int i=1;i<=m.groupCount();i++){
String token = m.group( i );
System.out.println(token);
}
}
Second
public static void main(String[] args) {
String input = "20150101";
String pattern = "(\\d{4})(\\d{2})(\\d{2})";
Pattern r = Pattern.compile(pattern);
Matcher m = r.matcher(input);
m.find();
for(int i=1;i<=m.groupCount();i++){
String token = m.group( i );
System.out.println(token);
}
}
private static final Pattern DATE_PATTERN = Pattern.compile("^([\\d]{4})([\\d]{2})([\\d]{2})$");
public static Optional<String[]> split(String str) {
final Matcher matcher = DATE_PATTERN.matcher(str);
if (matcher.find()) {
final String[] array = new String[3];
array[0] = matcher.group(1);
array[1] = matcher.group(2);
array[2] = matcher.group(3);
return Optional.of(array);
}
return Optional.empty();
}
i'm trying to get float or integer from the string.
for that here is my test cases..
1) str="IN100RINR" Output = 100;
2) str="IN100RINR" Output = 100;
3) str="100INR" Output = 100;
4) str="100.50INR" Output = 100.50;
5) str="IN100.50R" Output = 100.50;
6) str="INR100.50" Output = 100.50;
7) str="INR100INRINR20.500INR" Output = 100
This all working in my program but case 7 is not working..it returns 100.500
here is my code...
Pattern pattern = Pattern.compile("(\\d+)");
String str="INR100INRINR20.500INR", amount="", decimal="";
if(str.contains(".")){
String temp= str.substring(str.indexOf(".")+1);
Matcher matcher = pattern.matcher(temp);
if(matcher.find()){
decimal = matcher.group();
}
}
Matcher matcher = pattern.matcher(str);
if(matcher.find()){
if(decimal != ""){
amount=matcher.group()+"."+decimal;
}else{
amount = matcher.group();
}
System.out.println(Float.valueOf(amount));
}
You could use a simple matcher/find method to do something like that:
Pattern pattern = Pattern.compile("\\d+(?:\\.\\d+)?"); // Match int or float
String str="INR100INRINR20.500INR";
Matcher matcher = pattern.matcher(str);
if(matcher.find()){
System.out.println(matcher.group());
}
ideone demo
i wanna extract a part of url which is at the middle of it, by using regex in java
this is what i tried,mostly the problem to detect java+regexis that its in the middle of last part of url and i have no idea how to ignore the characters after it, my regex just ignoring before it:
String regex = "https://www\\.google\\.com/(search)?q=([^/]+)/";
String url = "https://www.google.com/search?q=regex+java&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en-US:official&client=firefox-a";
Pattern pattern = Pattern.compile (regex);
Matcher matcher = pattern.matcher (url);
if (matcher.matches ())
{
int n = matcher.groupCount ();
for (int i = 0; i <= n; ++i)
System.out.println (matcher.group (i));
}
}
the result should be regex+java or even regex java . but my code didnt work out...
Try:
String regex = "https://www\\.google\\.com/search\\?q=([^&]+).*";
String url = "https://www.google.com/search?q=regex+java&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en-US:official&client=firefox-a";
Pattern pattern = Pattern.compile (regex);
Matcher matcher = pattern.matcher (url);
if (matcher.matches ())
{
int n = matcher.groupCount ();
for (int i = 0; i <= n; ++i)
System.out.println (matcher.group (i));
}
The result is:
https://www.google.com/search?q=regex+java&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en-US:official&client=firefox-a
regex+java
EDIT
Replacing all pluses before printing:
for (int i = 0; i <= n; ++i) {
String str = matcher.group (i).replaceAll("\\+", " ");
System.out.println (str);
}
String regex = "https://www\\.google\\.com/?(search)\\?q=([^&]+)?";
String url = "https://www.google.com/search?q=regex+java&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en-US:official&client=firefox-a";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(url);
while (matcher.find()) {
System.out.println(matcher.group());
}
This should do your job.
How to read the string from the position (Example)5 to the end of the string in java.
QRegExp StripType(re, Qt::CaseInsensitive);
int p = StripType.indexIn(line, 0);
int len = StripType.matchedLength();
String tmp += line.mid(len);
How to convert QT into java
Where re is in the above code is regular expression and i want to covert the above into java i have tried
String s =pattern.toString();
int pos = s.indexOf(line);
Matcher matcher = Pattern.compile(re).matcher(line);
if (matcher.find()) {
System.out.println(matcher.group());
} else {
System.out.println("String contains no character other than that");
}
len = matcher.start();
But its not working correct
Thanks in Advance
To begin with you should add the Pattern.CASE_INSENSITIVE flag.
Matcher matcher = Pattern.compile(re, Pattern.CASE_INSENSITIVE).matcher(line);