string break for java - java

I am coding in java and I have string like this [A⋈ (B⋈C)]⋈[D⋈ (E⋈F)] I want to split it in a way that I get (B⋈C) in different sub string and (E⋈F) in different string.How can I do that?
I try to do it by regex and string split but it do not work for me.
String[] items = str.split(Pattern.quote("(?=-)"));
ArrayList<String> itemList = new ArrayList<String>();
for (String item : items)
{
itemList.add(item);
}
System.out.println(itemList);

You can this Regex: "\\([A-Za-z⋈]*\\)"
String mData = "[A⋈ (B⋈C)]⋈[D⋈ (E⋈F)] ";
Pattern pattern = Pattern.compile("\\([A-Za-z⋈]*\\)");
Matcher m = pattern.matcher(mData);
while (m.find()) {
System.out.println(mData.substring(m.start(), m.end()));
}

I think it should work : Try this regex "(.{3})"

Related

Transform string format to list - it is not a json format string

How to transform a string with this format into a list?
[[["Census_county_divisions","Populated_places_in_the_United_States","Populated_places_by_country","Geography_by_country","Geography_by_place","Geography","Main_topic_classifications"]],[["example","text","thanks"]],[["name","surname","age"]]]
From that string I would like to have 3 lists:
List 1:
"Census_county_divisions","Populated_places_in_the_United_States","Populated_places_by_country","Geography_by_country","Geography_by_place","Geography","Main_topic_classifications"
List 2:"example","text","thanks"
List 3:"name","surname","age"
I have tried different approachs do process this string, with split, with method StringUtils.substringBetween, with indexOf, with regex, with Json Parser.... I always get an error, is it an easier way out??
Comments: I don't see this string as a Json format, since the Json format would be "name":"John", If I'm wrong, please let me know how I could process it as a Json....
I have tried also with JsonParser and had the Exception in thread "main" java.lang.IllegalStateException: Not a JSON Object:
[[["Census_county_divisions","Popula
I write this code:
remove the [[[,]]] strings
replace the ]],[[ for | character
split the string
///The String to convert
String arg = "[[[\"Census_county_divisions\",....
[[\"example\",\"text\",\"thanks\"]],[[\"name\",\"surname\",\"age\"]]]";
System.out.println(arg);
////Replace
arg = arg.replace("[[[", "");
arg = arg.replace("]],[[", "|");
arg = arg.replace("]]]", "");
System.out.println(arg);
////Split
String[] array=arg.split("\\|");
List<String> list = Arrays.asList(array);
///Verify
for(String s: list) {
System.out.println(s);
}
Regards
This worked for me. I only trimmed the start and end character set and then split it into 3 different strings which yielded the lists.
str = new StringBuilder(str).replace(0, 4, "")
.reverse().replace(0, 4, "")
.reverse()
.toString();
String[] arr1 = str.split("\"\\]\\],\\[\\[\"");
List<String> list1 = Arrays.asList(arr1[0]
.split("\",\""));
List<String> list2 = Arrays.asList(arr1[1]
.split("\",\""));
List<String> list3 = Arrays.asList(arr1[2]
.split("\",\""));
Maybe you could use Regex to extract everything inside the "[[" "]]" and trimming the "[" and "]" at the start and the end of each group. After that, you could split the result and put it into a List.
Here a simple example:
List<List<String>> lists = new ArrayList<>();
String string = "[[[\"Census_cunty_divisions\",\"Populated_places_in_the_United_States\",\"Populated_places_by_country\",\"Geography_by_country\",\"Geography_by_place\",\"Geography\",\"Main_topic_classifications\"]],[[\"example\",\"text\",\"thanks\"]],[[\"name\",\"surname\",\"age\"]]]";
Pattern pattern = Pattern.compile("\\[\\[(.*?)\\]\\]");
Matcher matcher = pattern.matcher(string);
String proccesed;
while (matcher.find()) {
for (int i = 1; i <= matcher.groupCount(); i++) {
proccesed = StringUtils.strip(matcher.group(i), "[");
proccesed = StringUtils.strip(proccesed, "]");
lists.add(Arrays.asList(proccesed.split(",")));
}
}
int i = 0;
for(List<String> stringList : lists){
System.out.printf("List # %s \n", i);
for(String elementOfList:stringList){
System.out.printf("Element %s \n", elementOfList);
}
i++;
}
Here you will have a dynamic list depending on the initial String.
I've used the org.apache.commons commons-text library to strip the matches.
I hope it's useful.

Regex find optional entries in Java

I'm currently working on some stuff with regex and struggel alot with regex latetly.
I wanted to build some script engine, for that I need to load some presets:
example:
create <Type> [after;before;at;between(2);<Integer>, <DateTime>, <Date>, <Time>, <String>] : Creator
edit <Type> [after;before;at;between(2);<Integer>, <DateTime>, <Date>, <Time>, <String>]
run [<File>, <Command>]
So I want to make sure I can read <Type> [after;before;at;between(2);<Integer>, <DateTime>, <Date>, <Time>, <String>] and [<File>, <Command>].
For the understanding:
NAME <IMPORTANT_PARAMETER> [TEXT_PARAMETER(AMOUNT_OF_OPTIONAL_PAREMETER);<OPTIONAL_PARAMETER(S)>].
In this example I used 'command names' as IMPORTANT_PARAMETER.
For the first rule I made this regex: \<(\w+)\>(?:\s+\[(?:(.*;))(.*)\])?(?:\s+\:\s+(\w+))? and it kinda works within my code:
Pattern p = Pattern.compile("\\<(\\w+)\\>(?:\\s+\\[(?:(.*;))(.*)\\])?(?:\\s+\\:\\s+(\\w+))?");
Matcher m = p.matcher(parameters);
if(m.matches()){
Command command2 = new Command(command);
command2.addParameter(new Parameter(m.group(1)));
String text = m.group(2);
String[] texts = null;
if(text != null){
texts = text.split(";");
command2.addTexts(Arrays.asList(texts));
}
String type = m.group(3);
String[] types = null;
if(type != null){
types = type.split(", ");
for (String string : types) {
Pattern pTypes = Pattern.compile("\\<(?:(\\w+))\\>");
Matcher mTypes = pTypes.matcher(string);
if(mTypes.matches()){
command2.addParameter(new Parameter(mTypes.group(1), true));
}
}
}
String className = m.group(4);
if(className != null){
command2.addClassName(className);
}
commandslist.add(command2);
}
I tried to use \[\<(\w+)\>(?:,\s+\<(\w+)\>)+\] but it only worked for two entries -> example run [<File>, <Command>]. It would be better having a "list" of those optional elements [<File>, <Command>]. So in the end I want to have m.group(1) = File; m.group(2) = Command; m.group(3) = blablabla; and so on.
I hope I could show you my problem good enough, hit me with questions if there is anything more to explain.
Here is a link to the regexr: REGEXR or regex101: REGEX101
Thanks for helping :)
My suggestion is to match the stuff between the words you are after:
public static void main (String[] args) {
final String STR1 = "run [<File>, <Command1>, <Command2>, <Command3>]";
final String STR2 = "run [<File>, <Command1>, <Command2>, <Command3>, <Command4>]";
System.out.println(parse(STR1));
System.out.println(parse(STR2));
}
private static List parse(String str) {
List<String> list = new ArrayList<>();
Pattern p = Pattern.compile("(?:\\G,\\s+|^run\\s+\\[(?:<\\w+>,\\s+)+?)<(\\w+)>");
Matcher m = p.matcher(str);
while (m.find()) {
list.add(m.group(1));
}
return list;
}
which results in the output:
[Command1, Command2, Command3]
[Command1, Command2, Command3, Command4]

Parse string value from URL

I have a string (which is an URL) in this pattern https://xxx.kflslfsk.com/kjjfkskfjksf/v1/files/media/93939393hhs8.jpeg
now I want to clip it to this
media/93939393hhs8.jpeg
I want to remove all the characters before the second last slash /.
i'm a newbie in java but in swift (iOS) this is how we do this:
if let url = NSURL(string:"https://xxx.kflslfsk.com/kjjfkskfjksf/v1/files/media/93939393hhs8.jpeg"), pathComponents = url.pathComponents {
let trimmedString = pathComponents.suffix(2).joinWithSeparator("/")
print(trimmedString) // "output = media/93939393hhs8.jpeg"
}
Basically, I'm removing everything from this Url expect of last 2 item and then.
I'm joining those 2 items using /.
String ret = url.substring(url.indexof("media"),url.indexof("jpg"))
Are you familiar with Regex? Try to use this Regex (explained in the link) that captures the last 2 items separated with /:
.*?\/([^\/]+?\/[^\/]+?$)
Here is the example in Java (don't forget the escaping with \\:
Pattern p = Pattern.compile("^.*?\\/([^\\/]+?\\/[^\\/]+?$)");
Matcher m = p.matcher(string);
if (m.find()) {
System.out.println(m.group(1));
}
Alternatively there is the split(..) function, however I recommend you the way above. (Finally concatenate separated strings correctly with StringBuilder).
String part[] = string.split("/");
int l = part.length;
StringBuilder sb = new StringBuilder();
String result = sb.append(part[l-2]).append("/").append(part[l-1]).toString();
Both giving the same result: media/93939393hhs8.jpeg
string result=url.substring(url.substring(0,url.lastIndexOf('/')).lastIndexOf('/'));
or
Use Split and add last 2 items
string[] arr=url.split("/");
string result= arr[arr.length-2]+"/"+arr[arr.length-1]
public static String parseUrl(String str) {
return (str.lastIndexOf("/") > 0) ? str.substring(1+(str.substring(0,str.lastIndexOf("/")).lastIndexOf("/"))) : str;
}

Check if String contains hashtag word

How to effective check, if input string contains hashtag word and get it?
Live example:
String input = "My name is #George and I like #Java."
String[] hashTag = getHashTag(input);
Results are: #George, #Java
Thank you for your reply.
Try to use this:
String input = "My name is #George and I like #Java.";
Pattern patt = Pattern.compile("(#\\w+)\\b");
Matcher match = patt.matcher(input);
List<String> matStr =new ArrayList<String>();
while (match.find()) {
matStr.add(match.group(1));
}
System.out.println("Results are: "+matStr.get(0)+" , " +matStr.get(1));
Output:
Results are: #George , #Java
String input = "My name is #George and I like #Java.";
Matcher m = Pattern.compile("(#\\w+)\\b",Pattern.CASE_INSENSITIVE).matcher(input);
while(m.find()){
System.out.println(m.group(1));
}
Output :
#George
#Java
Try this LINK
This can be done using regex:
private static String[] getHashTag(String str) {
ArrayList<String> allMatches = new ArrayList<String>();
Matcher m = Pattern.compile("(#\\w+)\\b").matcher(str);
while (m.find()) {
allMatches.add(m.group());
}
return allMatches.toArray(new String[0]);
}
This can be called with your line: String[] hashTag = getHashTag(input);.

Java. Regular Expression. How Parse?

As input parameters I can have two types of String:
codeName=SomeCodeName&codeValue=SomeCodeValue
or
codeName=SomeCodeName
without codeValue.
codeName and codeValue are the keys.
How can I use regular expression to return the key's values? In this example it would return only SomeCodeName and SomeCodeValue.
I wouldn't bother with a regex for that. String.split with simple tokens ('&', '=') will do the job.
String[] args = inputParams.split("&");
for (String arg: args) {
String[] split = arg.split("=");
String name = split[0];
String value = split[1];
}
Consider using Guava's Splitter
String myinput = "...";
Map<String, String> mappedValues =
Splitter.on("&")
.withKeyValueSeparator("=")
.split(myinput);
The simple way is to split the source string first and then to run 2 separate regular expressions against 2 parts.
Pattern pCodeName = Pattern.compile("codeName=(.*)");
Pattern pCodeValue = Pattern.compile("codeValue=(.*)");
String[] parts = str.split("\\&");
Matcher m = pCodeName.matcher(parts[0]);
String codeName = m.find() ? m.group(1) : null;
String codeValue = null;
if (parts.length > 1) {
m = pCodeValue.matcher(parts[1]);
codeValue = m.find() ? m.group(1) : null;
}
}
But if you want you can also say:
Pattern p = Pattern.compile("codeName=(\\w+)(\\&codeValue=(\\w+))?");
Matcher m = p.matcher(str);
String codeName = null;
String codeValue = null;
if (m.find()) {
codeName = m.group(1);
codeValue = m.groupCount() > 1 ? m.group(2) : null;
}

Categories

Resources