Get Date Pattern from the random string - java

I have a scenario to return "YYYYMMDD" from "v={YYYYMMDD}" with out using String literal methods like split, substring etc.
Is there anyway I can use the dateUtil methods or any regex?
Thanks.

Using regex -
String input = "v={YYYYMMDD}";
String regex = "^v=\\{([^}]{8})\\}$";
Matcher m = Pattern.compile(regex).matcher(input);
if (m.find()) {
System.out.println("Date pattern - " + m.group(1));
} else {
System.out.println("No date found!");
}
Result -
Date pattern - YYYYMMDD

Related

How can I get the string element from the format string using regular expression?

My input string is like this :
String msgs="<InfoStart>\r\n"
+ "id:1234\r\n"
+ "phone:912119882\r\n"
+ "info_type:1\r\n"
+<InfoEnd>\r\n"
+"<InfoStart>\r\n"
+ "id:5678\r\n"
+ "phone:912119881\r\n"
+ "info_type:1\r\n"
+<InfoEnd>\r\n";
Now I can use the regular expression to get the info array :
private static Pattern patter= Pattern.compile("InfoStart>([\\s\\S]*?)<InfoEnd>");,But how to get the id,phone using regular expression?I try to write the code,but it fail,how to fix it?
private static Pattern infP = Pattern.compile("<InfoStart>([\\s\\S]*?)<InfoEnd>");
private static Pattern lineP = Pattern.compile(".*?\r\n");
final java.util.regex.Matcher matcher = patter.matcher(msgs);
while (matcher.find()){
String item = matcher.group(1);
Matcher matcherLine = lineP.matcher(item);
while(matcherLine.find()){
if(matcherLine.groupCount()>0){
String value= matcherLine.group(1);
int firstIndex=value.indexOf(":");
System.out.println("key:"+value.substring(0, firstIndex)+"value:"+value.substring(firstIndex+1));
}
}
}
Perhaps you can try this:
Pattern xmlPattern = Pattern.compile("<InfoStart>\\s+id:(\\d+)\\s+phone:(\\d+)\\s+info_type:(\\d+)\\s+<InfoEnd>");
Matcher matcher = xmlPattern.matcher(msgs);
while (matcher.find()) {
System.out.println(matcher.group(1));
System.out.println(matcher.group(2));
System.out.println(matcher.group(3));
}
The output:
1234
912119882
1
5678
912119881
1
But still I have to as say as Tim Biegeleisen mentioned, you'd better use other way around to parse a XML string.
Besides, your input string is incorrect, it should be:
String msgs="<InfoStart>\r\n"
+ "id:1234\r\n"
+ "phone:912119882\r\n"
+ "info_type:1\r\n"
+ "<InfoEnd>\r\n" // you lack an open double quote;
+"<InfoStart>\r\n"
+ "id:5678\r\n"
+ "phone:912119881\r\n"
+ "info_type:1\r\n"
+ "<InfoEnd>\r\n"; // you lack an open double quote;

Java regex how to get value in brackets. Matcher does not work

I have a problem with matcher
String s = "termination:[2018-06-13T00:00:00 TO 2018-06-13T23:59:59] "
Pattern r = Pattern.compile(".*(termination:\[(.+?) TO (.+?)\]?).*");
Matcher m = r.matcher(s);
if (m.find( )) {
String startDate = m.group(2);
String endDate = m.group(3);
}
But the end date is 2 instead of 2018-06-13T23:59:59
Any idea why?
You have a slight typo in your regex. This is the corrected one:
.*(termination:\[(.+?) TO (.+?)\]).*
Your mistake is an extra ? after \]. This makes the closing bracket optional, which in turn causes the lazy (.+?) to match only one character.
Just use a more accurate pattern:
String s = "termination:[2018-06-13T00:00:00 TO 2018-06-13T23:59:59] ";
Pattern r = Pattern.compile(".*(termination:\\[(\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2})\\sTO\\s(\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2})\\]?).*");
Matcher m = r.matcher(s);
if (m.find()) {
String startDate = m.group(2);
String endDate = m.group(3);
System.out.println(startDate + " ---- " + endDate);
}
... and it will print this:
2018-06-13T00:00:00 ---- 2018-06-13T23:59:59

How can i replace this?

How can I replace this
String str = "KMMH12DE1433";
String pattern = "^[a-z]{2}([0-9]{2})[a-z]{1,2}([0-9]{4})$";
String str2 = str.replaceAll(pattern, "repl");
Log.e("Founded_words2",str2);
What I got: KMMH12DE1433
What I want: MH12DE1433
Try it like this using a proper java.util.regex.Pattern and a java.util.regex.Matcher:
String str = "KMMH12DE1433";
//Make the pattern, case-insensitive using (?i)
Pattern pattern = Pattern.compile("(?i)[a-z]{2}([0-9]{2})[a-z]{1,2}([0-9]{4})");
//Create the Matcher
Matcher m = pattern.matcher(str);
//Check if we find anything
if(m.find()) {
//Use what you found - with proper capturing groups you
//gain access to parts of your pattern as needed
System.out.println("Found this: " + m.group());
}
If you just want to remove the first two characters and if the first two characters will always be uppercase letters:
String str = "KMMH12DE1433";
String pattern = "^[A-Z]{2}";
String str2 = str.replaceAll(pattern, "");
Log.e("Output string: ", str2);
try this :
String a = "KMMH12DE1433";
String pattern = "^[A-Z]{2}";
String rs = a.replaceAll(pattern,"");
Please change like this
String ans=str.substring(0);

Regex match and get string by pattern JAVA

I have here a pattern for email
Pattern pattern = Pattern.compile("^[A-Za-z0-9+_.-]+#(.+)$");
also i have a String contains messages
String message = "Han hannibal#domain.com im 20 years old, i just came here to say nothing..";
my problem is when matching pattern to string i get nothing. here what i do
Pattern pattern = Pattern.compile(pattern);
Matcher m = pattern.matcher(message);
if(m.find()) {
Log.d("TAG", m.group(1));
}else {
Log.d("TAG", "No email found on string");
}
i don't if my code was right but i just simply follow some article on fetching words on string using regex.
You need to remove the anchors from your regex.
Pattern pattern = Pattern.compile("\\b[A-Za-z0-9+_.-]+#(?:[^.\\s]+\\.)+\\w{2,4}\\b");
Example:
String message = "Han hannibal#domain.com im 20 years old, i just came here to say nothing..";
Pattern pattern = Pattern.compile("\\b[A-Za-z0-9+_.-]+#(?:[^.\\s]+\\.)+\\w{2,4}\\b");
Matcher m = pattern.matcher(message);
if(m.find())
{
System.out.println("TAG " + m.group());
}
else {
System.out.println("TAG " + "No email found on string");
}
Output:
TAG hannibal#domain.com

Splitting a string java

I have a string in format:
<+923451234567>: Hi here is the text.
Now I want to get the mobile number(without any non-alphanumeric characters) ie 923451234567 in the start of the string in-between < > symbols, and also the text ie Hi here is the text.
Now I can place a hardcoded logic, which I am currently doing.
String stringReceivedInSms="<+923451234567>: Hi here is the text.";
String[] splitted = cpaMessage.getText().split(">: ", 2);
String mobileNumber=MyUtils.removeNonDigitCharacters(splitted[0]);
String text=splitted[1];
How can I neatly get the required strings from the string with regular expression? So that I don't have to change the code whenever the format of the string changes.
String stringReceivedInSms="<+923451234567>: Hi here is the text.";
Pattern pattern = Pattern.compile("<\\+?([0-9]+)>: (.*)");
Matcher matcher = pattern.matcher(stringReceivedInSms);
if(matcher.matches()) {
String phoneNumber = matcher.group(1);
String messageText = matcher.group(2);
}
Use a regex that matches the pattern - <\\+?(\\d+)>: (.*)
Use the Pattern and Matcher java classes to match the input string.
Pattern p = Pattern.compile("<\\+?(\\d+)>: (.*)");
Matcher m = p.matcher("<+923451234567>: Hi here is the text.");
if(m.matches())
{
System.out.println(m.group(1));
System.out.println(m.group(2));
}
You need to use regex, the following pattern will work:
^<\\+?(\\d++)>:\\s*+(.++)$
Here is how you would use it -
public static void main(String[] args) throws IOException {
final String s = "<+923451234567>: Hi here is the text.";
final Pattern pattern = Pattern.compile(""
+ "#start of line anchor\n"
+ "^\n"
+ "#literal <\n"
+ "<\n"
+ "#an optional +\n"
+ "\\+?\n"
+ "#match and grab at least one digit\n"
+ "(\\d++)\n"
+ "#literal >:\n"
+ ">:\n"
+ "#any amount of whitespace\n"
+ "\\s*+\n"
+ "#match and grap the rest of the string\n"
+ "(.++)\n"
+ "#end anchor\n"
+ "$", Pattern.COMMENTS);
final Matcher matcher = pattern.matcher(s);
if (matcher.matches()) {
System.out.println(matcher.group(1));
System.out.println(matcher.group(2));
}
}
I have added the Pattern.COMMENTS flag so the code will work with the comments embedded for future reference.
Output:
923451234567
Hi here is the text.
You can get your phone number by just doing :
stringReceivedInSms.substring(stringReceivedInSms.indexOf("<+") + 2, stringReceivedInSms.indexOf(">"))
So try this snippet:
public static void main(String[] args){
String stringReceivedInSms="<+923451234567>: Hi here is the text.";
System.out.println(stringReceivedInSms.substring(stringReceivedInSms.indexOf("<+") + 2, stringReceivedInSms.indexOf(">")));
}
You don't need to split your String.

Categories