Java Finding String regex - java

I tried to get these date 22-APR-16 11.00.00.000000 and 22-APR-16 10.30.00.000000.
My codes are there but it cant find ,how can I do?
String pattern = "(Başlangıç Tarihi:\\s+)([0-9/:]+\\s+[0-9:]+)(.*)\\s+(Bitiş Tarihi:\\s+)([0-9/:]+\\s+[0-9:]+)(.*)";
Pattern r = Pattern.compile(pattern);
String text = "Başlangıç Tarihi: 22-APR-16 11.00.00.000000 AM Bitiş Tarihi: 22-APR-16 10.30.00.000000 PM";
Matcher m = r.matcher(text);
if(m.find())
{
String startDate = m.group(2);
String endDate = m.group(5);
System.out.println("Start Date : " + startDate);
System.out.println("End Date : " + endDate);
}

KISS
String pattern = "(Başlangıç Tarihi:\\s+)(\\d+-[A-Za-z]+-\\d+\\s[\\d.]+)(.*)\\s+(Bitiş Tarihi:\\s+)(\\d+-[A-Za-z]+-\\d+\\s[\\d.]+)";
Ideone Demo
Moreover, you can just use
(\\d+-[A-Za-z]+-\\d+\\s[\\d.]+)
and find all the matches using loop and store it an array or arraylist. Every even element will be start date and odd element will be end date

Related

Extract multiple dates (dd-MMM-yyyy format) from a string in java

I have searched everywhere for this but couldn't get a specific solution, and the documentation also didn't cover this. So I want to extract the start date and end date from this string "1-Mar-2019 to 31-Mar-2019". The problem is I'm not able to extract both the date strings.
I found the closest solution here but couldn't post a comment asking how to extract values individually due to low reputation: https://stackoverflow.com/a/8116229/10735227
I'm using a regex pattern to look for the occurrences and to extract both occurrences to 2 strings first.
Here's what I tried:
Pattern p = Pattern.compile("(\\d{1,2}-[a-zA-Z]{3}-\\d{4})");
Matcher m = p.matcher(str);
while(m.find())
{
startdt = m.group(1);
enddt = m.group(1); //I think this is wrong, don't know how to fix it
}
System.out.println("startdt: "+startdt+" enddt: "+enddt);
Output is:
startdt: 31-Mar-2019 enddt: 31-Mar-2019
Additionally I need to use DateFormatter to convert the string to date (adding the trailing 0 before single digit date if required).
You can catch both dates simply calling the find method twice, if you only have one, this would only capture the first one :
String str = "1-Mar-2019 to 31-Mar-2019";
String startdt = null, enddt = null;
Pattern p = Pattern.compile("(\\d{1,2}-[a-zA-Z]{3}-\\d{4})");
Matcher m = p.matcher(str);
if(m.find()) {
startdt = m.group(1);
if(m.find()) {
enddt = m.group(1);
}
}
System.out.println("startdt: "+startdt+" enddt: "+enddt);
Note that this could be used with a while(m.find()) and a List<String to be able to extract every date your could find.
If your text may be messy, and you really need to use a regex to extract the date range, you may use
String str = "Text here 1-Mar-2019 to 31-Mar-2019 and tex there";
String startdt = "";
String enddt = "";
String date_rx = "\\d{1,2}-[a-zA-Z]{3}-\\d{4}";
Pattern p = Pattern.compile("(" + date_rx + ")\\s*to\\s*(" + date_rx + ")");
Matcher m = p.matcher(str);
if(m.find())
{
startdt = m.group(1);
enddt = m.group(2);
}
System.out.println("startdt: "+startdt+" enddt: "+enddt);
// => startdt: 1-Mar-2019 enddt: 31-Mar-2019
See the Java demo
Also, consider this enhancement: match the date as whole word to avoid partial matches in longer strings:
Pattern.compile("\\b(" + date_rx + ")\\s*to\\s*(" + date_rx + ")\\b")
If the range can be expressed with - or to you may replace to with (?:to|-), or even (?:to|\\p{Pd}) where \p{Pd} matches any hyphen/dash.
You can simply use String::split
String range = "1-Mar-2019 to 31-Mar-2019";
String dts [] = range.split(" ");
System.out.println(dts[0]);
System.out.println(dts[2]);

Convert date number to words using java

I want to convert a date to words. For example: 12/12/2012 --> twelve twelve two thousand twelve and I already made number to word converter. But now I have problem to print it out.
Here my code:
String patternString = "\\d{2}/\\d{2}/\\d{4}"; // date regex
Pattern pattern = Pattern.compile(patternString); // pattern compiling
Matcher matcher = pattern.matcher(nom); // matching with pattern with input text from user
if (matcher.find()) {
String get_data = matcher.group();
if(get_data.contains("/")){ // check either has "/" slash or not
String parts[] = get_data.split("[/]"); // split process
String get_day = parts[0]; // day will store in first array
String get_month = parts[1]; // month will store in second array
String get_year = parts[2]; // year will store in third array
String s = NumberConvert.convert(Integer.parseInt(get_day))
+ NumberConvert.convert(Integer.parseInt(get_month))
+ NumberConvert.convert(Integer.parseInt(get_year));
String replace = matcher.replaceAll(s); // replace number to words
System.out.println(replace);
}
} else {...}
Input text from user:
12/12/2012 +++ 23/11/2010
But the result print only first pattern and next pattern also replace with value of first pattern too.
twelve twelve two thousand twelve +++ twelve twelve two thousand twelve
Please suggest me the solution
An immediate solution to your problem would be to use Matcher.replaceFirst(), instead of Matcher.replaceAll(), since you only want the first date pattern to be replaced with your written version of the date.
String replace = matcher.replaceFirst(s);
If you would like to be able to process each numeric date one at a time, you can do so in a left-to-right fashion using this code:
String patternString = "\\d{2}/\\d{2}/\\d{4}";
Pattern pattern = Pattern.compile(patternString);
Matcher matcher = pattern.matcher(nom);
String output = "";
while (matcher.find()) {
String get_data = matcher.group();
String parts[] = get_data.split("/");
String get_day = parts[0];
String get_month = parts[1];
String get_year = parts[2];
String s = NumberConvert.convert(Integer.parseInt(get_day)) +
NumberConvert.convert(Integer.parseInt(get_month)) +
NumberConvert.convert(Integer.parseInt(get_year));
if (output.equals("")) {
output = s;
}
else {
output += " +++ " + s;
}
String replace = matcher.replaceFirst("");
matcher = pattern.matcher(replace);
}
After each iteration, the above code resets the Matcher using a string from which the previous date matched has been removed. This lets you "eat" one date at a time, from left to right, building the human readable date output as you go along.

Java regular expression to parse between dates?

I am struggling to come up with a regular expression to parse some logs that are very unstructured but always have a date that begins with the line that needs to be parsed.
An example is 2015-9-20 05:20:22 lots of log data and then the next date for the next line. So I would basically need to parse everything from the starting date until the next date.
2015-9-20 05:20:22 lots of log data
2015-9-20 05:21:22 lots of new log data
Is it possible to parse this using regular expression?
So I would basically need to parse everything from the starting date until the next date.
If you want to match lines beggining with one date, or beggining with the following day (startDate + 1 day), you can use it in your pattern as literal text.
Using the dates in your example:
^(?:2015-9-20|2015-9-21) .*
Code:
// Instantiate a Date object
Date startDate = new GregorianCalendar(2015, 8, 20).getTime();
// Calculate end date (+1 day)
Calendar endDate = Calendar.getInstance();
endDate.setTime(startDate);
endDate.add(Calendar.DATE, 1); // Add 1 day
// format dates the same way logs use
SimpleDateFormat ft =
new SimpleDateFormat ("y-M-d");
// Create regex
String datesRegex = "^(?:" + ft.format(startDate) + "|" + ft.format(endDate.getTime()) + ") .*";
DEMO
If you want to get all lines from one date to another, and not only those starting with a given date, you should match with the .DOTALL modifier:
^2015-9-20 .*?(?=^2015-9-21 |\z)
Code:
// Create regex
String datesRegex = "^" + ft.format(startDate) + " .*?(?=^" + ft.format(endDate.getTime()) + " |\\z)";
// Compile
Pattern.compile(datesRegex, Pattern.MULTILINE | Pattern.DOTALL);
DEMO
Assuming you're reading the file line-by-line, this should work for you:
^\d{4}-\d{1,2}-\d{2} \d{2}:\d{2}:\d{2} (.*)$
Code example:
String line = "2015-9-20 05:20:22 log data" + System.lineSeparator();
String pattern = "^\\d{4}-\\d{1,2}-\\d{2} \\d{2}:\\d{2}:\\d{2} (.*)$";
// Create a Pattern object
Pattern r = Pattern.compile(pattern);
// Now create matcher object.
Matcher m = r.matcher(line);
if (m.find()) {
System.out.println("Value after timestamp is: " + m.group(1));
} else {
System.out.println("NO MATCH");
}
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegexMatches
{
public static void main( String args[] ){
String s1 = "2015-9-20 05:20:22 lots of log data";
String s2 = "2015-9-20 05:21:22 lots of new log data";
String pattern = "(\\d{4})-(0?\\d|1[0-2])-([012]\\d|3[01]) ([01]?\\d|2[0-4]):([0-5]?\\d):([0-5]?\\d)";
Pattern r = Pattern.compile(pattern);
Matcher m = r.matcher(s1); //same for s2
if(m.find())
System.out.println("True");
else
System.out.println("False");
}
}
Output: True

Regex: How to include character of "if" condition

I'm making a date extractor using regex in java. Problem is that date is 20-05-2014 and my program is extracting 0-5-14. In short, how can I get the character on which I'm checking the second character of date?
int count = 0;
String data = "HellowRoldsThisis20-05-2014. farhan_rock#gmail.comHellowRoldsThisis.farhan#gmail.com";
String regexOfDate = "((?<=[0])[1-9]{2})|((?<=[12])[0-9])|((?<=[3])[01])\\.\\-\\_((?<=[0])[1-9])|((?<=[1])[0-2])\\.\\-\\_((?<=[2])[0-9]{4})"; \\THE PROBLEM
String[] extractedDate = new String[1000];
Pattern patternDate = Pattern.compile(regexOfDate);
Matcher matcherDate = patternDate.matcher(data);
while(matcherDate.find()){
System.out.println("Date "+count+"Start: "+matcherDate.start());
System.out.println("Date "+count+"End : "+matcherDate.end());
extractedDate[count] = data.substring(matcherDate.start(), matcherDate.end());
System.out.println("Date Extracted: "+extractedDate[count]);
}
You can try the regular expression:
// (0[1-9]|[12][0-9]|[3][01])[._-](0[1-9]|1[0-2])[._-](2[0-9]{3})
"(0[1-9]|[12][0-9]|[3][01])[._-](0[1-9]|1[0-2])[._-](2[0-9]{3})"
A single regex o match valid dates is awful.
I'd do:
String regexOfDate = "(?<!\\d)\\d{2}[-_.]\\d{2}[-_.]\\d{4}(?!\\d)";
to extract the potential date, then test if it is valid.

Regexp matching in group with special characters

I have a String that is something like this:
A20130122.0000+0000-0015+0000_name
Then I would like to extract this information:
The 20130122.0000+0000-0015+0000 that will be parsed to a date later on.
And the final part which is name.
So I am using in Java something like this:
String regexpOfdate = "[0-9]{8}\\.[0-9]{4}\\+[0-9]{4}-[0-9]{4}\\+[0-9]{4}";
String regexpOfName = "\\w+";
Pattern p = Pattern.compile(String.format("A(%s)_(%s)", regexpOfdate, regexpOfName));
Matcher m = p.matcher(theString);
String date = m.group(0);
String name = m.group(1);
But I am getting a java.lang.IllegalStateException: No match found
Do you know what I am doing wrong?
You aren't calling Matcher#find or Matcher#matches methods after this line:
Matcher m = p.matcher(theString);
Try this code:
Matcher m = p.matcher(theString);
if (m.find()) {
String date = m.group(1);
String name = m.group(2);
System.out.println("Date: " + date + ", name: " + name);
}
Matcher#group will throw IllegalStateException if the matcher's regex hasn't yet
been applied to its target text, or if the previous application was not successful.
Matcher#find applies the matcher's regex to the current region of the matcher's target text, returning a Boolean indicating whether a match is found.
Refer
You can try this :
String theString="A20130122.0000+0000-0015+0000_name";
String regexpOfdate = "([0-9]{8})\\.[0-9]{4}\\+[0-9]{4}-[0-9]{4}\\+[0-9]{4}";
String regexpOfName = "(\\w+)";
Pattern p = Pattern.compile(String.format("A(%s)_(%s)", regexpOfdate, regexpOfName));
Matcher m = p.matcher(theString);
if(m.find()){
String date = m.group(2);
String name = m.group(3);
System.out.println("date: "+date);
System.out.println("name: "+name);
}
OUTPUT
date: 20130122
name: name
Refer Grouping in REGEX

Categories

Resources