I am currently using a regex to interprete the below format.
File Name : File_[TimeStamp:yyyyMMdd_HHmm].csv and regex for interpreting the format is : "\[TimeStamp(:[^\[\]]+)?\]" .
it will return a String with timestamp value : File_20230120_1716.csv
The sample code which support interpreting the timestamp format in the filename:
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Test {
private static final String DEFAULT_FORMAT= "yyyyMMddHHmmss";
private static final String TIMESTAMP_REGEX = "\\[TimeStamp(:[^\\[\\]]+)?\\]";
public static String processFileName(String value, String regex ) {
LocalDateTime dateTime=LocalDateTime.now();
Matcher matcher=Pattern.compile(regex, Pattern.DOTALL).matcher(value);
while(matcher.find()) {
DateTimeFormatter dtf=DateTimeFormatter.ofPattern(DEFAULT_FORMAT);
if(matcher.group(1)!=null) {
dtf=DateTimeFormatter.ofPattern(matcher.group(1).replaceFirst(":", ""));
}
value=value.replace(matcher.group(0), dtf.format(dateTime));
}
return value;
}
public static void main(String args[]) {
String str=processFileName("File_[TimeStamp:yyyyMMdd_HHmm].csv", TIMESTAMP_REGEX);
System.out.println(str);
}
}
Output: File_20230120_1716.csv
Currently I can use File_[TimeStamp:yyyyMMdd_HHmm].csv or File_[TimeStamp].csv formats to interprete with above regex.
But I wanted to modify the above "\[TimeStamp(:[^\[\]]+)?\]" sothat it can interpret ZonId also in this format : File_[TimeStam|Asia\Tokyo:yyyyMMdd_HHmm].csv.
Based upon ZoneId from the input I will modify the LocalDateTime value.
Related
public class Test {
public static void main(String[] args) {
String str = "WELCOMEWELCOME";
// find the occurance of 'CO' in the given string using stream API
}
}
You can use Stream API and RegEx API as shown below to meet this requirement:
import java.util.regex.MatchResult;
import java.util.regex.Pattern;
public class Main {
public static void main(String args[]) {
// find the occurance of 'CO' in the given string using stream API
String str = "WELCOMEWELCOME";
String substring = "CO";
System.out.println(getSubstringCount(str, substring));
}
static long getSubstringCount(String str, String substring) {
return Pattern.compile(substring)
.matcher(str)
.results()
.map(MatchResult::group)
.count();
}
}
Output:
2
ONLINE DEMO
I'm trying to escape japanese xml to display as normal japanese string and not unicode xml. I can't use apache.commons.lang3 and only apache.commons.lang is preferred. If you have any other suggestions not in this library feel free to share. Thanks in advance!
final String xmlToEscape = "言語が良くない";
final String escapedXml = StringEscapeUtils.escapeXml(xmlToEscape);
Prints:
言 ;語 ;が ;良 ;く ;な ;い ;
Should print:
言語が良くない
StringEscapeUtils.escapeXml() in apache.commons.lang always escapes non-ASCII characters.
If you don't want to escape Japanese characters, you have to pass only the ASCII characters in the string to StringEscapeUtils.escapeXml() like this:
package org.example;
import java.util.Arrays;
import org.apache.commons.lang.StringEscapeUtils;
public class Test {
public static void main(String[] args) {
// You will get "言語が良くない <ABC>"
System.out.println(StringEscapeUtils.escapeXml("言語が良くない <ABC>"));
// You will get "言語が良くない <ABC>"
System.out.println(escapeXml("言語が良くない <ABC>"));
}
public static String escapeXml(String str) {
return Arrays.stream(str.split("")).map(s -> escapeCharacter(s)).collect(StringBuilder::new, StringBuilder::append, StringBuilder::append).toString();
}
public static String escapeCharacter(String str) {
if (str.matches("\\p{ASCII}")) {
return StringEscapeUtils.escapeXml(str);
} else {
return str;
}
}
}
I tried with
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Rejex {
public static final String REGEX_CONSTANT="^[a-zA-Z][a-zA-Z0-9&\\-()$##!^*=|'{}:.%_+?`~<>[]]*$";
public static void main(String[] args) {
System.out.println(validateRegex(REGEX_CONSTANT,"dd[]"));
}
public static boolean validateRegex(String regex, String value)
{
Pattern patternObjects = Pattern.compile(regex);
Matcher matcherObject = patternObjects.matcher(value);
return matcherObject.matches();
}
}
It returns false, i want [] should be there in expression, i want expression starts with string and accepts all symbols, numbers,everything, with [], {}, () also
Try with the following regex :
"^[a-zA-Z][a-zA-Z0-9&\\-()$##!^*=|'{}:.%_+?`~<>\\[\\]]*$"
[ and ] need to be escaped within your custom character class (https://docs.oracle.com/javase/8/docs/api/java/util/regex/Pattern.html)
I am reading about design pattern and would like to know what would be a good way to eliminate the below code duplication for the format function.
Assume I have the below code, what approach is best to take?. I can create an abstract class and inherit function or pull out the function into a separate static and make reference.
public interface Generator{
generate()
}
public class test1 implementes Generator{
generate()
public static string FormatDate(){
String date_s = " 2011-01-18 00:00:00.0";
SimpleDateFormat dt = new SimpleDateFormat("yyyy-mm-dd hh:mm:ss");
Date date = dt.parse(date_s);
SimpleDateFormat dt1 = new SimpleDateFormat("yyyy-mm-dd");
System.out.println(dt1.format(date));
}
}
public class test2 implementes Generator{
generate()
public static string FormatDate(){
String date_s = " 2011-01-18 00:00:00.0";
SimpleDateFormat dt = new SimpleDateFormat("yyyy-mm-dd");
Date date = dt.parse(date_s);
SimpleDateFormat dt1 = new SimpleDateFormat("yyyymmdd");
System.out.println(dt1.format(date));
}
}
You can write a separate util class which will have this static function and refer it in test1 and test2.
You could write something like this
Mainclass
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Testclass implements Generator, DateFormatter {
public static void main(String[] args){
Testclass stack = new Testclass();
try {
stack.generate();
stack.formatDate("2011-01-18 00:00:00.0", "yyyy-mm-dd hh:mm:ss", "yyyy-mm-dd");
stack.generate();
stack.formatDate("2011-01-18 00:00:00.0", "yyyy-mm-dd", "yyyymmdd");
} catch (ParseException e) {
// TODO add some more Exceptionhandling here
System.out.println("The given String could not be parsed.");
e.printStackTrace();
}
}
#Override
public void generate() {
}
#Override
public void formatDate(String dateString, String parserFormat, String outputFormat) throws ParseException {
SimpleDateFormat dt = new SimpleDateFormat(parserFormat);
Date date = dt.parse(dateString);
SimpleDateFormat dt1 = new SimpleDateFormat(outputFormat);
System.out.println(dt1.format(date));
}
}
Interfaces
Generator:
public interface Generator {
void generate();
}
DateFormatter:
import java.text.ParseException;
public interface DateFormatter {
void formatDate(String dateString, String parserFormat, String outputFormat) throws ParseException;
}
One thing i want to add:
Dont use Date or SimpleDateFormat, if you are using Java 8 use DateTime
Oracle about DateTime
If you cant use Java 8, use the force
You can create an abstract class with 2 abstract methods String getFormat1() and String getFormat2().
Then add a non-static method formatDate() which contains your code above, and calls getFormat1() and getFormat2() when creating the SimpleDateFormat objects.
You can then create 2 classes that extend the abstract class and define the abstract methods returning appropriate values ("yyyy-mm-dd hh:mm:ss" and "yyyy-mm-dd" for the first and the others for the second).
If you need more formats, just create another subclass.
You also want to stop using static all the time, since that'll prevent you from using many design patterns as well as cause other problems.
This is the Template Method Pattern.
I need to use a replace function in Java:
string.replace(myString,"");
myString values are for example javascript:r(0), javascript:r(23), javascript:l(5) etc. Just number is always different and there is r or l letter. What's the regular expression to match it? Thanks
(FIXED) The regex you want is
"javascript:[rl]\\(\\d+\\)"
NOTE: The outer quotes aren't really part of the regex; they are part of the Java string you pass to Pattern.compile or directly to replace.
Here is a complete program you can try:
import java.util.regex.Pattern;
import java.util.regex.Matcher;
public class PatternExample {
private static final Pattern JS_PATTERN = Pattern.compile("javascript:[rl]\\(\\d+\\)");
private static final String[] MESSAGES = {
"Good javascript:r(5)",
"Good javascript:l(50003843)",
"Good javascript:r(1123934)",
"Bad javascript:|(5)",
"Bad javascript:r(53d)",
"Bad javascript:l()",
};
public static void main(String[] args) {
for (String s : MESSAGES) {
Matcher matcher = JS_PATTERN.matcher(s);
System.out.println(matcher.replaceAll(""));
}
}
}
Here is another version of the above program that calls replaceAll directly on the string instead of pre-compiling the pattern:
public class PatternExample {
private static final String[] MESSAGES = {
"Good javascript:r(0)",
"Good javascript:l(50003843)",
"Good javascript:r(1123934)",
"Bad javascript:|(5)",
"Bad javascript:r(53d)",
"Bad javascript:l()",
};
public static void main(String[] args) {
for (String s : MESSAGES) {
System.out.println(s.replaceAll("javascript:[rl]\\(\\d+\\)", ""));
}
}
}
The following regex will match it:
javascript:[rl]\(\d+\)