How to convert a Java string against a pattern regex? - java

I want to convert a component version (for example 2.6.0) to this form G02R06C00. I think it's possible with regex and pattern with Java but I don't found how. I have seen examples to match a string against a pattern (in this case the regex would be G\d\dR\d\dC\d\d I guess), but I don't find how to convert a string to other string with a pattern. Thanks in advance.
What I have done is:
String gorocoVersion = version.replaceAll("(\\d*)\\.(\\d*)\\.(\\d*)", "G$1R$2C$3");
that produces G2R6C0, it's missing yet to add 0 to match 2 digits.
So, I have to split input string before and I can't anymore use replaceAll, that's why I was looking for more clever option that automatically add 0 before simple digit too according to the output pattern G\d\dR\d\dC\d\d, like we can find in word or excel
Something that works with snapshot version but it's very ugly :
/**
* #return version in G00R00C00(-SNAPSHOT) format.
*/
private String formatVersion() {
String gorocoVersion = "";
if (StringUtils.isNotBlank(version)) {
String[] versionTab = version.split("\\.");
String partG = String.format("%02d", Integer.parseInt(versionTab[0]));
String partR = String.format("%02d", Integer.parseInt(versionTab[1]));
String partC = versionTab[2];
String snapshotSuffix = "-SNAPSHOT";
if (partC.endsWith(snapshotSuffix)) {
partC = String.format("%02d",
Integer.parseInt(partC.substring(0, partC.indexOf('-')))) + snapshotSuffix;
} else {
partC = String.format("%02d", Integer.parseInt(partC));
}
gorocoVersion = "G" + partG + "R" + partR + "C" + partC;
}
return gorocoVersion;
}

Try this method:
private static String formatVersion(String version) {
Pattern pattern = Pattern.compile("^(\\d+)\\.(\\d+)\\.(\\d+)(-SNAPSHOT)*$");
Matcher matcher = pattern.matcher(version);
if (matcher.find()) {
return String.format("G%02dR%02dC%02d", Integer.parseInt(matcher.group(1)), Integer.parseInt(matcher.group(2)), Integer.parseInt(matcher.group(3)));
} else {
throw new IllegalArgumentException("Unsupported version format");
}
}
Output for the version param value 2.6.0:
G02R06C00

Thanks a lot Ilya, here my final code which accepts SNAPSHOT and also x.y versions :
private String formatVersion(String version) {
if (StringUtils.isNotBlank(version)) {
Pattern pattern = Pattern.compile("^(\\d+)\\.(\\d+)\\.?(\\d+)?(-\\w*)?");
Matcher matcher = pattern.matcher(version);
if (matcher.find()) {
return String.format("G%02dR%02dC%02d%s", Integer.parseInt(matcher.group(1)),
Integer.parseInt(matcher.group(2)),
matcher.group(3) != null ? Integer.parseInt(matcher.group(3)) : 0,
matcher.group(4) != null ? matcher.group(4) : "");
}
}
return version;
}
With this code 2.6 gives G02R06C00 and 2.6.1-SNAPSHOT gives G02R06C01-SNAPSHOT, perfect :)

Related

String Manipulation in java 1.6

String can be like below. Using java1.6
String example = "<number>;<name-value>;<name-value>";
String abc = "+17005554141;qwq=1234;ddd=ewew;otg=383";
String abc = "+17005554141;qwq=123454";
String abc = "+17005554141";
I want to remove qwq=1234 if present from String. qwq is fixed and its value can VARY like for ex 1234 or 12345 etc
expected result :
String abc = "+17005554141;ddd=ewew;otg=383";
String abc = "+17005554141"; \\removed ;qwq=123454
String abc = "+17005554141";
I tried through
abc = abc.replaceAll(";qwq=.*;", "");
but not working.
I came up with this qwq=\d*\;? and it works. It matches for 0 or more decimals after qwq=. It also has an optional parameter ; since your example seems to include that this is not always appended after the number.
I know the question is not about javascript, but here's an example where you can see the regex working:
const regex = /qwq=\d*\;?/g;
var items = ["+17005554141;qwq=123454",
"+17005554141",
"+17005554141;qwq=1234;ddd=ewew;otg=383"];
for(let i = 0; i < items.length; i++) {
console.log("Item before replace: " + items[i]);
console.log("Item after replace: " + items[i].replace(regex, "") + "\n\n");
}
You can use regex for removing that kind of string like this. Use this code,
String example = "+17005554141;qwq=1234;ddd=ewew;otg=383";
System.out.println("Before: " + example);
System.out.println("After: " + example.replaceAll("qwq=\\d+;?", ""));
This gives following output,
Before: +17005554141;qwq=1234;ddd=ewew;otg=383
After: +17005554141;ddd=ewew;otg=383
.* applies to multi-characters, not limited to digits. Use something that applies only to bunch of digits
abc.replaceAll(";qwq=\\d+", "")
^^
Any Number
please try
abc = abc.replaceAll("qwq=[0-9]*;", "");
If you don't care about too much convenience, you can achieve this by just plain simple String operations (indexOf, replace and substring). This is maybe the most legacy way to do this:
private static String replaceQWQ(String target)
{
if (target.indexOf("qwq=") != -1) {
if (target.indexOf(';', target.indexOf("qwq=")) != -1) {
String replace =
target.substring(target.indexOf("qwq="), target.indexOf(';', target.indexOf("qwq=")) + 1);
target = target.replace(replace, "");
} else {
target = target.substring(0, target.indexOf("qwq=") - 1);
}
}
return target;
}
Small test:
String abc = "+17005554141;qwq=1234;ddd=ewew;otg=383";
String def = "+17005554141;qwq=1234";
System.out.println(replaceQWQ(abc));
System.out.println(replaceQWQ(def));
outputs:
+17005554141;ddd=ewew;otg=383
+17005554141
Another one:
abc.replaceAll(";qwq=[^;]*;", ";");
You must to use groups in replaceAll method.
Here is an example:
abc.replaceAll("(.*;)(qwq=\\d*;)(.*)", "$1$3");
More about groups you can find on: http://www.vogella.com/tutorials/JavaRegularExpressions/article.html

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;

regex matcher check in if logic not working

Hi, you can see my code below. I have some strings Country, rank and grank in my code, initially they will be null, but if regex is mached, it should change the value. But even if regex is matched it is not changing the value it is always null. If I remove all if statements and append the string it works fine, but if match is not found it is throwing an exception. Please let me know how can I check this in if logic.
System.err.println(content);
Pattern c = Pattern.compile("NAME=\"(.*)\" RANK");
Pattern r = Pattern.compile("\" RANK=\"(.*)\"");
Pattern gr = Pattern.compile("\" TEXT=\"(.*)\" SOURCE");
Matcher co = c.matcher(content);
Matcher ra = r.matcher(content);
Matcher gra = gr.matcher(content);
co.find();
ra.find();
gra.find();
String country = null;
String Rank = null;
String Grank = null;
if (co.matches()) {
country = co.group(1);
}
if (ra.matches()) {
Rank = ra.group(1);
}
if (gra.matches()) {
Grank = gra.group(1);
}
You have to escape a single \ - use double \\ then it should work.
Tried this?
while (co.find()) {
System.out.print("Start index: " + co.start());
System.out.print(" End index: " + co.end() + " ");
System.out.println(co.group());
}
Personally I can't make your program work with / without the if so it's not a problem of logic but just a problem that it doesn't match the string for me
So I changed it to get something working, maybe you can use it :)
String content = "NAME=\"salut\" RANK=\"pouet\" TEXT=\"text\" SOURCE";
System.out.println(content);
System.out.println(content.replaceAll(("NAME=\"(.*)\"\\sRANK=\"(.*)\"\\sTEXT=\"(.*)\" SOURCE"), "$1---$2---$3"));
Output
NAME="salut" RANK="pouet" TEXT="text" SOURCE
salut---pouet---text

Validate File name using regex in java

Here i want to validate filename using regex in java. i implemented below code but this is not works for me for 3rd type file.
Can i check prefix and extenstion in regex ???
My validate filename looks like these 3 ways
1) prefix_digit.digit.extenstion example : AB_1.1.fuij (Here fuij is my extension)
2) prefix_digit.digit.digit.extenstion example : AB_1.1.1.fuij
3) prefix_digit.digit.B/P.digit.extensionexample : AB_1.1.B.1.fuij
Only these 3 types of file valid. 3rd one is beta and pilot version files. if beta and pilot version file is there than is should be like this which i mentioned above
I am going to write some valid and invalid files
**Valid :**
AB_1.1.fuij
AB_1.4.fuij
AB_1.1.1.fuij
AB_1.1.B.1.fuij
AB_3.4.P.7.fuij
***Invalid :***
AB_0.1.fuij
AB_1.B.1.1.fuij(B/P should be place on 3rd always)
AB_1.2.B.0.fuij
CODE :
import java.util.ArrayList;
import java.util.regex.Pattern;
public class democlass {
/**
* Test harness.
*/
public static void main(String[] args) {
ArrayList<String> demoversion = new ArrayList<String>();
System.out.println("Result >>>>>>>>>>>> "
+isFileValid("AB_1.1.fuij"));
System.out.println("Result >>>>>>>>>>>> "
+isFileValid("AB_1.B.fuij"));
System.out.println("Result >>>>>>>>>>>> "
+isFileValid("AB_1.1.1.fuij"));
System.out.println("Result >>>>>>>>>>>> "
+isFileValid("AB_1.P.1.1.fuij"));
System.out.println("Result >>>>>>>>>>>> "
+isFileValid("AB_1.1.B.1.fuij"));
}
private static boolean isFileValid(String input)
{
String regexFinalBugFix = "^\\d+\\.\\d+\\.\\d+$";
String regexFinal = "^\\d+\\.\\d+$";
String regexBetaPilot = "^\\d+\\.\\d+\\.\\[BP]+\\.\\d+$";
final Pattern pattern1 = Pattern.compile(regexFinal);
final Pattern pattern2 = Pattern.compile(regexBetaPilot);
final Pattern pattern3 = Pattern.compile(regexFinalBugFix);
String inputVersion = null;
int suffixIndex = input.lastIndexOf(".");
int prefixIndex = input.lastIndexOf("_");
if (suffixIndex > 0 && prefixIndex > 0) {
inputVersion = input.substring(prefixIndex + 1,
suffixIndex);
String prefixString1 = input.substring(0, 3);
String suffixString1 = input.substring(suffixIndex);
if(prefixString1.equals("AB_") && suffixString1.equals(".fuij"))
{
if (pattern1.matcher(inputVersion).matches()
|| pattern2.matcher(inputVersion).matches()
|| pattern3.matcher(inputVersion).matches()) {
return true;
}
return false;
}
return false;
}
return false;
}
}
OUTPUT :
Result >>>>>>>>>>>> true
Result >>>>>>>>>>>> false
Result >>>>>>>>>>>> true
Result >>>>>>>>>>>> false
Result >>>>>>>>>>>> false : It should be valid but it is false, why??
Your regexBetaPilot is wrong: you are escaping the opening bracket of the [BP] class. Try this instead:
String regexBetaPilot = "^\\d+\\.\\d+\\.[BP]+\\.\\d+$";
You can easily combine all three patterns into a single pattern:
String regex = "\\d+\\.(\\d+\\.([BP]+\\.)?)?\\d+";
You don't need the anchors (^ and $). Since you are using matches() instead of find(), it will always try to match the entire string.
EDIT I left in the + after [BP] because that's what you had in your original code. However, if you want to match a single B or P, then you should remove the + from the pattern.
You are escaping the opening bracket of [BP], so it tries to find a [ in the string.
This works:
String regexBetaPilot = "^\\d+\\.\\d+\\.[BP]+\\.\\d+$";
Something like this should work with AB being static:
Regular Expression: AB_\d+\.\d+((\.\d){0,1}|\.[BP]\.\d+)\.fuij
as a Java string AB_\\d+\\.\\d+((\\.\\d){0,1}|\\.[BP]\\.\\d+)\\.fuij
This misses two of your listed invalids, but I was unsure why they should be invalid. I can halep more if you explain the rules for success / failure better?
You can simplify your regular expression to
AB_\d+\.\d+(?:(?:\.[BP])?\.\d+)?\.fuij
This matches AB_digits.digits. Then comes an optional .digits, .B.digits or .P.digits. And finally matches .fuij. From your examples, there might be only a single B or P. If you wish to match multiple Bs and Ps, just add the + again.
And then your isFileValid() function might be reduced to
private static boolean isFileValid(String input)
{
final String re = "AB_\\d+\\.\\d+(?:(?:\\.[BP])?\\.\\d+)?\\.fuij";
final Pattern pattern = Pattern.compile(re);
return pattern.matcher(input).matches();
}

Why does this regex fail?

I have a PCL file and open it with Notepad ++ to view the source code (with PCL Viewer I see the final results but I need to view the source also).
Please see Lab Number and the rest of the characters. I am able to extract Lab Number and its code with this regex:
private static String PATTERN_LABNUMBER = "Lab Number[\\W\\D]*(\\d*)";
and it gives me:
0092616281
I now want to extract Date Reported and I use this regex (after a lot of other tries):
private static String PATTERN_DATE_REPORTED =
"Date Reported[\\W\\D]*(\\d\\d/\\d\\d/\\d\\d\\d\\d \\d\\d:\\d\\d)";
but it does NOT find it in the PCL file.
I've also tried with:
private static String PATTERN_DATE_REPORTED =
"Date Reported[\\W\\D]*([0-9]{2}/[0-9]{2}/[0-9]{4} [0-9]{2}:[0-9]{2})";
but the same not found result...
Do you see where I am missing something in this last regex?
Thanks a lot!
UPDATE:
I use this java code to extract Lab number and Date Reported:
public String extractWithRegEx(String regextype, String input) {
String matchedString = null;
if (regextype != null && input != null) {
Matcher matcher = Pattern.compile(regextype).matcher(input);
if (matcher.find()) {
System.out.println("Matcher found for regextype "+regextype);
matchedString = matcher.group(0);
if (matcher.groupCount() > 0) {
matchedString = matcher.group(1);
}
}
}
return matchedString;
}
Here is the code to accomplish what you want..
Pattern pattern = Pattern.compile("Date Reported.*(\\d{2}/\\d{4} \\d{2}:\\d{2})$", Pattern.MULTILINE);
String st = "date dfdsfsd fgfd gdfgfdgdf gdfgdfg gdfgdf 3232/22/2010 23:34\n"+
"dsadsadasDate Reported gdfgfd gdfgfdgdf gdfgdfg gdfgdf 3232/22/2010 23:34";
Matcher matcher = pattern.matcher(st);
while (matcher.find()) {
System.out.println(matcher.group(1));
}

Categories