I'm creating this method to search the text only for the Service Order number, this information can come anywhere in the text because it is an open field for typing, I only need the first value found if you have more than one service order.
example text:
Please check service order number 1-202012345678 for vehicle repair toyota Corolla red
Could someone help me find the error?
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Encontra_Ordem {
private static final Pattern PATTERN = Pattern.compile(".*([1#][-#]\\d{12}).*");
public static String buscaordemnotexto(String texto) {
String valor = "";
Matcher matcher = PATTERN.matcher(texto);
if(matcher.matches() && matcher.groupCount() == 1){
String numerodaordem = matcher.group(1);
valor += numerodaordem;
}
return valor;
}
}
With the given data I'd suggest this solution:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Encontra_Ordem {
private static final Pattern PATTERN = Pattern.compile("\\b\\d-\\d{12}\\b");
public static String buscaordemnotexto(String texto) {
String valor = "";
Matcher matcher = PATTERN.matcher(texto);
if (matcher.find()) {
String numerodaordem = matcher.group();
valor += numerodaordem;
}
return valor;
}
public static void main(String[] args) {
System.out.println(buscaordemnotexto("asda sd asd asd asd sa 2-202012345678 ttttttt 1-202012345678"));
}
}
Output
2-202012345678
Explanation
Here's the breakdown of the RegEx I wrote (click on it to make it more readable):
I also used matcher.find() inside an if block so that the content of matcher.group() will be the first RegEx match.
The find() Method
Put simply, the find() method tries to find the
occurrence of a regex pattern within a given string. If multiple
occurrences are found in the string, then the first call to find()
will jump to the first occurrence. Thereafter, each subsequent call to
the find() method will go to the next matching occurrence, one by one.
Sources
https://regexr.com
https://www.baeldung.com/java-matcher-find-vs-matches
Related
I have a string which contains multiple url starting from http and https I need to fetch all those url and put into a list.
I have tried below code.
List<String> httpLinksList = new ArrayList<>();
String hyperlinkRegex = "((http:\/\/|https:\/\/)?(([a-zA-Z0-9-]){2,}\.){1,4}([a-zA-Z]){2,6}(\/([a-zA-Z-_\/\.0-9#:?=&;,]*)?)?)";
String synopsis = "This is http://stackoverflow.com/questions and https://test.com/method?param=wasd The code below catches all urls in text and returns urls in list";
Pattern pattern = Pattern.compile(hyperlinkRegex);
Matcher matcher = pattern.matcher(synopsis);
while(matcher.find()){
System.out.println(matcher.find()+" "+matcher.group(1)+" "+matcher.groupCount()+" "+matcher.group(2));
httpLinksList.add(matcher.group());
}
System.out.println(httpLinksList);
I need below result
[http://stackoverflow.com/questions,
https://test.com/method?param=wasd]
But getting below output
[https://test.com/method?param=wasd]
This regex will match all the valid urls, including FTP and other
String urlRegex = "((https?|ftp|gopher|telnet|file):((//)|(\\\\))+[\\w\\d:##%/;$()~_?\\+-=\\\\\\.&]*)";
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class xmlValue {
public static void main(String[] args) {
String text = "This is http://stackoverflow.com/questions and https://test.com/method?param=wasd The code below catches all urls in text and returns urls in list";
System.out.println(extractUrls(text));
}
public static List<String> extractUrls(String text)
{
List<String> containedUrls = new ArrayList<String>();
String urlRegex = "((https?|ftp|gopher|telnet|file):((//)|(\\\\))+[\\w\\d:##%/;$()~_?\\+-=\\\\\\.&]*)";
Pattern pattern = Pattern.compile(urlRegex, Pattern.CASE_INSENSITIVE);
Matcher urlMatcher = pattern.matcher(text);
while (urlMatcher.find())
{
containedUrls.add(text.substring(urlMatcher.start(0),
urlMatcher.end(0)));
}
return containedUrls;
}
}
Output:
[http://stackoverflow.com/questions,
https://test.com/method?param=wasd]
credits #BullyWiiPlaza
So I know this is not exactly what you asked since you are specifically looking for regex, but I thought this would fun to try out with an indexOf variant. I will leave it here as an alternative to the regex someone comes up with:
public static void main(String[] args){
String synopsis = "This is http://stackoverflow.com/questions and https://test.com/method?param=wasd The code below catches all urls in text and returns urls in list";
ArrayList<String> list = splitUrl(synopsis);
for (String s : list) {
System.out.println(s);
}
}
public static ArrayList<String> splitUrl(String s)
{
ArrayList<String> list = new ArrayList<>();
int spaceIndex = 0;
while (true) {
int httpIndex = s.indexOf("http", spaceIndex);
if (httpIndex < 0) {
break;
}
spaceIndex = s.indexOf(" ", httpIndex);
if (spaceIndex < 0) {
list.add(s.substring(httpIndex));
break;
}
else {
list.add(s.substring(httpIndex, spaceIndex));
}
}
return list;
}
All the logic is contained in the splitUrl(String s) method, it takes in a String as a parameter and outputs the ArrayList<String> of all the split urls.
It first searches for the index of any http and then the first space that occurs after the url and substrings the difference. It then uses the space it found as the second parameter in indexOf(String, int) to start searching the String beginning after the http that was already found so it does not repeat the same ones.
Additionally a case had to be made when the http is the final part of the String as there is no space afterward. This is done when the indexOf the space returns negative, I use substring(int) instead of substring(int, int) which will take the current location and substring the rest of the String.
The loop ends when either indexOf returns with a negative, though if the space returns negative it does that final substring operation before the break.
Output:
http://stackoverflow.com/questions
https://test.com/method?param=wasd
Note: As someone mentioned in the comments too, this implementation will work with non-Latin characters such as Hiragana too, which could be an advantage over regex.
I have this code:
String responseData = "http://xxxxx-f.frehd.net/i/world/open/20150426/1370235-005A/EPISOD-1370235-005A-016f1729028090bf_,892,144,252,360,540,1584,2700,.mp4.csmil/.m3u8";
"http://xxxxx-f.frehd.net/i/world/open/20150426/1370235-005A/EPISOD-1370235-005A-016f1729028090bf_,892,144,252,360,540,1584,2700,.mp4.csmil/.m3u8";
String pattern = ^(https://.*\.54325)$;
Pattern pr = Pattern.compile(pattern);
Matcher math = pr.matcher(responseData);
if (math.find()) {
// print the url
}
else {
System.out.println("No Math");
}
I want to print out the last string that starts with http and ends with .m3u8. How do I do this? I'm stuck. All help is appreciated.
The problem I have now is that when I find a math and what to print out the string, I get everything from responseData.
In case you need to get some substring at the end that is preceded by similar substrings, you need to make sure the regex engine has already consumed as many characters before your required match as possible.
Also, you have a ^ in your pattern that means beginning of a string. Thus, it starts matching from the very beginning.
You can achieve what you want with just lastIndexOf and substring:
System.out.println(str.substring(str.lastIndexOf("http://")));
Or, if you need a regex, you'll need to use
String pattern = ".*(http://.*?\\.m3u8)$";
and use math.group(1) to print the value.
Sample code:
import java.util.regex.*;
public class HelloWorld{
public static void main(String []args){
String str = "http://xxxxx-f.akamaihd.net/i/world/open/20150426/1370235-005A/EPISOD-1370235-005A-016f1729028090bf_,892,144,252,360,540,1584,2700,.mp4.csmil/index_0_av.m3u8" +
"EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=2795000,RESOLUTION=1280x720,CODECS=avc1.64001f, mp4a.40.2" +
"http://xxxxx-f.akamaihd.net/i/world/open/20150426/1370235-005A/EPISOD-1370235-005A-016f1729028090bf_,892,144,252,360,540,1584,2700,.mp4.csmil/index_6_av.m3u8";
String rx = ".*(http://.*?\\.m3u8)$";
Pattern ptrn = Pattern.compile(rx);
Matcher m = ptrn.matcher(str);
while (m.find()) {
System.out.println(m.group(1));
}
}
}
Output:
http://xxxxx-f.akamaihd.net/i/world/open/20150426/1370235-005A/EPISOD-1370235-005A-016f1729028090bf_,892,144,252,360,540,1584,2700,.mp4.csmil/index_6_av.m3u8
Also tested on RegexPlanet
I am making a program that allows the user to set variables and then use them in their messages such as %variable1% and I need a way of detecting the pattern which indicates a variable (%STRING%) . I am aware that I can use regex to find the patterns but am unsure how to use it to replace text.
I can also see a problem arising when using multiple variables in a single string as it may detect the space between 2 variables as a third variable
e.g. %var1%<-text that may be detected as a variable->%var2%, would this happen and is there any way to stop it?
Thanks.
A non-greedy regex would be helpful in extracting the variables which are within the 2 distinct % signs:
Pattern regex = Pattern.compile("\\%.*?\\%");
In this case if your String is %variable1%mndhokajg%variable2%" it should print
%variable1%
%variable2%
If your String is %variable1%variable2% it should print
%variable1%
%variable1%%variable2% should print
%variable1%
%variable2%
You can now manipulate/use the extracted variables for your purpose:
Code:
public static void main(String[] args) {
try {
String tag = "%variable1%%variable2%";
Pattern regex = Pattern.compile("\\%.*?\\%");
Matcher regexMatcher = regex.matcher(tag);
while (regexMatcher.find()) {
System.out.println(regexMatcher.group());
}
} catch (Exception e) {
e.printStackTrace();
}
}
Try playing around with different Strings, there can be invalid scenarios with % as part of the String but your requirement doesn't seem to be that stringent.
Oracle's tutorial on the Pattern and Matcher classes should get you started. Here is an example from the tutorial that you may be interested in:
import java.util.regex.Pattern;
import java.util.regex.Matcher;
public class ReplaceDemo {
private static String REGEX = "dog";
private static String INPUT =
"The dog says meow. All dogs say meow.";
private static String REPLACE = "cat";
public static void main(String[] args) {
Pattern p = Pattern.compile(REGEX);
// get a matcher object
Matcher m = p.matcher(INPUT);
INPUT = m.replaceAll(REPLACE);
System.out.println(INPUT);
}
}
Your second problem shouldn't happen if you use regex properly.
You can use this method for variable detection and their replacements from a passed HashMap:
// regex to detect variables
private final Pattern varRE = Pattern.compile("%([^%]+)%");
public String varReplace(String input, Map<String, String> dictionary) {
Matcher matcher = varRE.matcher( input );
// StringBuffer to hold replaced input
StringBuffer buf = new StringBuffer();
while (matcher.find()) {
// get variable's value from dictionary
String value = dictionary.get(matcher.get(1));
// if found replace the variable's value in input string
if (value != null)
matcher.appendReplacement(buf, value);
}
matcher.appendTail(buf);
return buf.toString();
}
I am quite bad at Java Regular expression so I hope you guys will help me.
String variable = "My life is better ";
String variable2 = "My life01 is better";
Now I have to write a code which would return true if the string has only "life"
So I should get TRUE only for variable not for variable2 because it has life but "01" too.
~thanks.
I have tried
if (variable.contains("life")){
System.out.println("TRUE");}
It return TRUE for both.
See solution :
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Test {
/**
* #param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Pattern p = Pattern.compile("\\blife\\b");
Matcher m = p.matcher("life0 is better");
boolean b = m.find();
System.out.println(b);
}
}
Use word boundary \b matches.
See the Java Pattern documentation for details.
Note that you may need to write it at \\b to get proper escaping. The pattern needs the string \b, which when used in .java code (and not read e.g. from a file!) needs to be written in Java-escaped form as "\\blife\\b".
Use the following regex: -
"\blife\b"
with Pattern and Matcher class. This will match for complete word. (\b denote word boundary)
You would have to use Matcher#find method, to check whether a string contains this pattern.
Note: - If you want to use String.matches, which would be appropriate here, than going with Pattern and Matcher, you would have to add .* in the front and the end. Because, String.matches matches the whole string.
For e.g: -
String str = "asdf life asdf";
System.out.println(str.matches("\\blife\\b")); // Prints false
System.out.println(str.matches(".*\\blife\\b.*")); // Prints true
In the second Regex, .* matches the string before and after life.
Use the String.matches() method and Regex \b to match word boundaries.
public class StringChecker {
String variable1 = "My life is better ";
String variable2 = "My life01 is better";
System.out.println("variable1: " + containsString(variable1));
System.out.println("variable1: " + containsString(variable2));
//Returns true if the string contains "life"
public boolean containsString(String s){
return s.matches(".*(\\blife\\b).*");
}
}
package com.rampukar;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class DemoRegx {
public static void main(String[] args) {
String email = "ram#example.com";
String EMAIL_PATTERN = "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*#" + "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
Pattern pt_mail = Pattern.compile(EMAIL_PATTERN);
Matcher mt_mail = pt_mail.matcher(email);
if (mt_mail.matches()) {
System.out.println("Valid");
} else {
System.out.println("In-Valid");
}
System.out.println("---------------------");
String name = "Ram Pukar";
Pattern pt_name = Pattern.compile("[a-zA-Z ]{2,}");
Matcher mt_name = pt_name.matcher(name);
if (mt_name.matches()) {
System.out.println("Valid");
} else {
System.out.println("In-Valid");
}
System.out.println("---------------------");
String user = "ram123";
Pattern pt_user = Pattern.compile("[a-zA-Z0-9]{2,}");
Matcher mt_user = pt_user.matcher(user);
if (mt_user.matches()) {
System.out.println("Valid");
} else {
System.out.println("In-Valid");
}
}
}
Hi I have been trying to learn RegExpresions using Java I am still at the begining and I wanted to start a little program that is given a string and outputs the syllabels split.This is what I got so far:
String mama = "mama";
Pattern vcv = Pattern.compile("([aeiou][bcdfghjklmnpqrstvwxyz][aeiou])");
Matcher matcher = vcv.matcher(mama);
if(matcher){
// the result of this should be ma - ma
}
What I am trying to do is create a pattern that checks the letters of the given word and if it finds a pattern that contains a vocale/consonant/vocale it will add a "-" like this v-cv .How can I achive this.
In the following example i matched the first vowel and used positive lookahead for the next consonant-vowel group. This is so i can split again if i have a vcvcv group.
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Test {
public static void main(String[] args) {
new Test().run();
}
private void run() {
String mama = "mama";
Pattern vcv =
Pattern.compile("([aeiou])(?=[bcdfghjklmnpqrstvwxyz][aeiou])");
Matcher matcher = vcv.matcher(mama);
System.out.println(matcher.replaceAll("$1-"));
String mamama = "mamama";
matcher = vcv.matcher(mamama);
System.out.println(matcher.replaceAll("$1-"));
}
}
Output:
ma-ma
ma-ma-ma
try
mama.replaceAll('([aeiou])([....][aeiou])', '\1-\2');
replaceAll is a regular expression method
Your pattern only matches if the String starts with a vocal. If you want to find a substring, ignoring the beginning, use
Pattern vcv = Pattern.compile (".*([aeiou][bcdfghjklmnpqrstvwxyz][aeiou])");
If you like to ignore the end too:
Pattern vcv = Pattern.compile (".*([aeiou][bcdfghjklmnpqrstvwxyz][aeiou]).*");