" 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);
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));
...
}
I have this:
import java.util.regex.*;
String regex = "(?<m1>(hello|universe))|(?<m2>(hello world))";
String s = "hello world";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(s);
while(matcher.find()) {
MatchResult matchResult = m.toMatchResult();
String substring = s.substring(matchResult.start(), matchResult.end());
System.out.println(substring);
}
The above only prints hello whereas I want it to print hello world.
One way to fix this is to re-order the groups in String regex = "(?<m2>(hello world))|(?<m1>(hello|universe))" but I don't have control over the regex I get in my case...
So what is the best way to find the longest match? An obvious way would be to check all possible substrings of s as mentioned here (Efficiently finding all overlapping matches for a regular expression) by length and pick the first but that is O(n^2). Can we do better?
Here is a way of doing it using matcher regions, but with a single loop over the string index:
public static String findLongestMatch(String regex, String s) {
Pattern pattern = Pattern.compile("(" + regex + ")$");
Matcher matcher = pattern.matcher(s);
String longest = null;
int longestLength = -1;
for (int i = s.length(); i > longestLength; i--) {
matcher.region(0, i);
if (matcher.find() && longestLength < matcher.end() - matcher.start()) {
longest = matcher.group();
longestLength = longest.length();
}
}
return longest;
}
I'm forcing the pattern to match until the region's end, and then I move the region's end from the rightmost string index towards the left. For each region's end tried, Java will match the leftmost starting substring that finishes at that region's end, i.e. the longest substring that ends at that place. Finally, it's just a matter of keeping track of the longest match found so far.
As a matter of optimization, and since I start from the longer regions towards the shorter ones, I stop the loop as soon as all regions that would come after are already shorter than the length of longest substring already found.
An advantage of this approach is that it can deal with arbitrary regular expressions and no specific pattern structure is required:
findLongestMatch("(?<m1>(hello|universe))|(?<m2>(hello world))", "hello world")
==> "hello world"
findLongestMatch("hello( universe)?", "hello world")
==> "hello"
findLongestMatch("hello( world)?", "hello world")
==> "hello world"
findLongestMatch("\\w+|\\d+", "12345 abc")
==> "12345"
If you are dealing with just this specific pattern:
There is one or more named group on the highest level connected by |.
The regex for the group is put in superfluous braces.
Inside those braces is one or more literal connected by |.
Literals never contain |, ( or ).
Then it is possible to write a solution by extracting the literals, sorting them by their length and then returning the first match:
private static final Pattern g = Pattern.compile("\\(\\?\\<[^>]+\\>\\(([^)]+)\\)\\)");
public static final String findLongestMatch(String s, Pattern p) {
Matcher m = g.matcher(p.pattern());
List<String> literals = new ArrayList<>();
while (m.find())
Collections.addAll(literals, m.group(1).split("\\|"));
Collections.sort(literals, new Comparator<String>() {
public int compare(String a, String b) {
return Integer.compare(b.length(), a.length());
}
});
for (Iterator<String> itr = literals.iterator(); itr.hasNext();) {
String literal = itr.next();
if (s.indexOf(literal) >= 0)
return literal;
}
return null;
}
Test:
System.out.println(findLongestMatch(
"hello world",
Pattern.compile("(?<m1>(hello|universe))|(?<m2>(hello world))")
));
// output: hello world
System.out.println(findLongestMatch(
"hello universe",
Pattern.compile("(?<m1>(hello|universe))|(?<m2>(hello world))")
));
// output: universe
just add the $ (End of string) before the Or separator |.
Then it check whether the string is ended of not. If ended, it will return the string. Otherwise skip that part of regex.
The below code gives what you want
import java.util.regex.*;
public class RegTest{
public static void main(String[] arg){
String regex = "(?<m1>(hello|universe))$|(?<m2>(hello world))";
String s = "hello world";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(s);
while(matcher.find()) {
MatchResult matchResult = matcher.toMatchResult();
String substring = s.substring(matchResult.start(), matchResult.end());
System.out.println(substring);
}
}
}
Likewise, the below code will skip hello , hello world and match hello world there
See the usage of $ there
import java.util.regex.*;
public class RegTest{
public static void main(String[] arg){
String regex = "(?<m1>(hello|universe))$|(?<m2>(hello world))$|(?<m3>(hello world there))";
String s = "hello world there";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(s);
while(matcher.find()) {
MatchResult matchResult = matcher.toMatchResult();
String substring = s.substring(matchResult.start(), matchResult.end());
System.out.println(substring);
}
}
}
If the structure of the regex is always the same, this should work:
String regex = "(?<m1>(hello|universe))|(?<m2>(hello world))";
String s = "hello world";
//split the regex into the different groups
String[] allParts = regex.split("\\|\\(\\?\\<");
for (int i=1; i<allParts.length; i++) {
allParts[i] = "(?<" + allParts[i];
}
//find the longest string
int longestSize = -1;
String longestString = null;
for (int i=0; i<allParts.length; i++) {
Pattern pattern = Pattern.compile(allParts[i]);
Matcher matcher = pattern.matcher(s);
while(matcher.find()) {
MatchResult matchResult = matcher.toMatchResult();
String substring = s.substring(matchResult.start(), matchResult.end());
if (substring.length() > longestSize) {
longestSize = substring.length();
longestString = substring;
}
}
}
System.out.println("Longest: " + longestString);
I am trying to remove zeros in value using regex(non capturing group). Does anyone have an idea?
Matcher matcher = Pattern.compile("(?:[0]+)?(\\S+)").matcher("00100");//.group(0));
//Matcher matcher = pattern.matcher(mydata);
if(matcher.matches()) {
System.out.println("value "+matcher.group(0));
}
str.replaceAll("^0+(?!$)", "")
If you want to remove leading zeroes, you can just use replaceAll:
String input = "00100";
input = input.replaceAll("^0+([^0].*)$", "$1");
Regex101
I found a solution to my own question:
public static void main(String[] args) {
extractValuesFromRegex("(?:0+|)(\\d+)", "00123");
extractValuesFromRegex("(?:0+|)(\\d+)", "123");
extractValuesFromRegex("(\\d+)", "00123");
extractValuesFromRegex("(\\d+)", "00123");
}
public static final String extractValuesFromRegex(String regex, String input) {
String extractevalue = input;
Matcher matcher = Pattern.compile(regex).matcher(input);
if (matcher.matches()) {
extractevalue = matcher.group(1);
}
return extractevalue;
}
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 have this code to
public static String ProcessTemplateInput(String input, int count) {
Pattern pattern = Pattern.compile("\\{([^\\}]+)\\}");
Matcher matcher = pattern.matcher(input);
while (matcher.find()) {
String newelem=SelectRandomFromTemplate(matcher.group(1), count);
}
return input;
}
Input is:
String s1 = "planets {Sun|Mercury|Venus|Earth|Mars|Jupiter|Saturn|Uranus|Neptune}{?|!|.} Is this ok? ";
Output example:
String s2="planets Sun, Mercury. Is this ok? ";
I would like to replace the {} set of templates with the picked value returned by the method. How do I do that in Java1.5?
Use appendReplacement/appendTail:
StringBuffer output = new StringBuffer();
while (matcher.find()) {
matcher.appendReplacement(output, SelectRandomFromTemplate(matcher.group(1), count));
}
matcher.appendTail(output);
return output.toString();