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
Related
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));
...
}
" 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 have a string as follows:
"This is #awesome #dude"
From this string i want to extract awesome and dude and create a string
output==> "awesome,dude"
So my code is like following:
Matcher matcher = Pattern.compile("(?<=#)\\w+").matcher(textStr);
while (matcher.find()){
mergedStr += matcher.group() +",";
}
But this creates an artifact in the end
output==> "awesome,dude," //<-- egghh comma.. in the end
What is a better way to solve this out.
Another approach:
boolean needComma = false;
while (blah, blah, blah) {
if (needComma) {
string += ",";
}
string += word;
needComma = true;
}
But there are a dozen different approaches.
This is one option:
Matcher matcher = Pattern.compile("(?<=#)\\w+").matcher(textStr);
while (matcher.find()){
if (!mergedStr.isEmpty())
mergedStr += ",";
mergedStr += matcher.group();
}
Here is another common approach:
Matcher matcher = Pattern.compile("(?<=#)\\w+").matcher(textStr);
StringBuilder sb = new StringBuilder();
while (matcher.find()){
sb.append(matcher.group()).append(",");
}
return sb.toString().replaceAll(",$", "");
If you don't want to use a regex, you could do it like this:
Matcher matcher = Pattern.compile("(?<=#)\\w+").matcher(textStr);
StringBuilder sb = new StringBuilder();
while (matcher.find()){
sb.append(matcher.group()).append(",");
}
if (sb.length() == 0) {
return "";
}
else {
return sb.toString().substring(0, sb.length() - 1);
}
A useful pattern that I often use for this kind of thing is to append the first item, and then append the remainder of the items preceded by the separator. This avoids unnecessary conditionals in loops or postprocessing to remove trailing separators.
I know, microoptimizations blah, blah, sixth circle of hell, blah, blah, but just including here for your amusement:
Matcher matcher = Pattern.compile("(?<=#)\\w+").matcher(textStr);
StringBuilder mergedStr = new StringBuilder();
if (matcher.find()) {
mergedStr.append(matcher.group());
while (matcher.find()) {
mergedStr.append(',').append(matcher.group());
}
}
return mergedStr.toString();
Also, I'm not 100% convinced that replacing a quadratic algorithm (string concatenation) with a linear algorithm (StringBuilder) qualifies as a microoptimization in the bad sense.
String input = "#awesome#dude";
List<String> strSplit = new ArrayList<String>();
String result = "";
Matcher matcher = Pattern.compile("(?<=#)\\w+").matcher(input);
while (matcher.find()){
strSplit.add(matcher.group());
}
for(int j = 0; j< strSplit.size(); j++){
result = result + strSplit.get(j);
if(j < strSplit.size() -1){
result = result+",";
}
}
System.out.println("Result : " + result);
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);