How to replace string with regex in Java or Clojure - java

I have some String like this:
"this is a string like #{aa} and#{bb}. "
"#{cc}this is another str#{dd}ing..."
I want to change these String like this:
"this is a string like ? and?." "aa" "bb"
"?this is another str?ing..." "cc" "dd"
I tried to use regular expressions to split these string and failed.
What should I do?

You can replace the string using a regex like this:
#\{(.*?)\}
Working demo
Then you have to grab the content from the capturing group and concatenate it to your string. I leave the logic for you :)

You can try like this:
final String regex = "#\\{(.*?)\\}";
final String string = "ere is some string #{sx} and #{sy}.";
final String subst = "?";
final Pattern pattern = Pattern.compile(regex);
final Matcher matcher = pattern.matcher(string);
while (matcher.find()) {
System.out.print(matcher.group(1) + " ");
}
final String result = matcher.replaceAll(subst);
System.out.println(result);

Related

How to Split with particular condition in Java?

Suppose I have a string like
"resources/json/04-Dec/someName_SomeTeam.json"
In above string I want just "04-Dec" part, this may change to "12-Jan" like this or any date with month with that format. How do I do this?
You can split using / and get the value 2
String text = "resources/json/04-Dec/someName_SomeTeam.json";
String[] split = text.split("\\/");
String result = split[2];//04-Dec
Or you can use patterns with this regex \d{2}\-\[A-Z\]\[a-z\]{2}:
String text = "resources/json/04-Dec/someName_SomeTeam.json";
String regex = "\\d{2}\\-[A-Z][a-z]{2}";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(text);
if (matcher.find()) {
System.out.println(matcher.group());
}

Finding the sub string

I have string like
Strig foo = "PrinciplexbxbxbxbxbxbTeachershshshshshhsClassdhdhdhhdhdhdList[something to be extracted]jdjdjdj"
I want output to be "something to be extracted" where regex of string is Principle*Teacher*Class*List[*]*.
After this make a new string
Strig foo = "PrinciplexbxbxbxbxbxbTeachershshshshshhsClassdhdhdhhdhdhdList[something new]jdjdjdj"
If you want to get something to be extracted then you can extract every thing between the two brackets \[(.*?)\], you can use Pattern like this :
String foo = ...;
String regex = "\\[(.*)\\]";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(foo);
if (matcher.find()) {
System.out.println(matcher.group(1));
}
If you want to replace it you can use replaceAll with the same regex :
foo = foo.replaceAll("\\[.*?\\]", "[something new]");
regex demo
If your String is always in the same format and contains the words Principle, Teachers and Class followed by [some text], then this is the Regex you need :
Principle\\w*Teachers\\w*Class\\w*\\[([\\s\\w]+)\\]\\w*
The matching group in \\[([\\s\\w]+)\\] will match the string you need.
Your code would be like this:
final String regex = "Principle\\w*Teachers\\w*Class\\w*\\[([\\s\\w]+)\\]\\w*";
final String string = "Strig foo = \"PrinciplexbxbxbxbxbxbTeachershshshshshhsClassdhdhdhhdhdhdList[something to be extracted]jdjdjdj\"\n";
string.replaceAll(regex, "something new");

Parse a string using Regular Expression in java

I am trying to parse a String using Regular expression. I have a content text:text and I want to parse the content from a string which has text:text.
Code:
String lines=" from:cal_date_d type:string relationship:many_to_one sql_on:${fact_customer.dw_update_date} = ${cal_date_d.dw_update_date}";
Pattern p = Pattern.compile("(\"?[\\w ]*)\\:(\"?([\\w]*)\"?)");
Matcher m = p.matcher(lines);
while(m.find()) {
String Column_Data=m.group(0);
System.out.println("Regex: "+Column_Data);
}
Ouput:
from:cal_date_d
type:string
relationship:many_to_one
sql_on:
Expected Output:
from:cal_date_d
type:string
relationship:many_to_one
sql_on:${fact_customer.dw_update_date} = ${cal_date_d.dw_update_date}
Try this pattern
([^\s]+( ?= ?[^\s]*)?)
https://regex101.com/r/c0q4W0/2
If you have string like "key1:value1 key2:value2..." then you can use this regex:
([^ ]*:[^ ]*)

How to split a long string in Java?

How to edit this string and split it into two?
String asd = {RepositoryName: CodeCommitTest,RepositoryId: 425f5fc5-18d8-4ae5-b1a8-55eb9cf72bef};
I want to make two strings.
String reponame;
String RepoID;
reponame should be CodeCommitTest
repoID should be 425f5fc5-18d8-4ae5-b1a8-55eb9cf72bef
Can someone help me get it? Thanks
Here is Java code using a regular expression in case you can't use a JSON parsing library (which is what you probably should be using):
String pattern = "^\\{RepositoryName:\\s(.*?),RepositoryId:\\s(.*?)\\}$";
String asd = "{RepositoryName: CodeCommitTest,RepositoryId: 425f5fc5-18d8-4ae5-b1a8-55eb9cf72bef}";
String reponame = "";
String repoID = "";
Pattern r = Pattern.compile(pattern);
Matcher m = r.matcher(asd);
if (m.find()) {
reponame = m.group(1);
repoID = m.group(2);
System.out.println("Found reponame: " + reponame + " with repoID: " + repoID);
} else {
System.out.println("NO MATCH");
}
This code has been tested in IntelliJ and runs without error.
Output:
Found reponame: CodeCommitTest with repoID: 425f5fc5-18d8-4ae5-b1a8-55eb9cf72bef
Assuming there aren't quote marks in the input, and that the repository name and ID consist of letters, numbers, and dashes, then this should work to get the repository name:
Pattern repoNamePattern = Pattern.compile("RepositoryName: *([A-Za-z0-9\\-]+)");
Matcher matcher = repoNamePattern.matcher(asd);
if (matcher.find()) {
reponame = matcher.group(1);
}
and you can do something similar to get the ID. The above code just looks for RepositoryName:, possibly followed by spaces, followed by one or more letters, digits, or hyphen characters; then the group(1) method extracts the name, since it's the first (and only) group enclosed in () in the pattern.

How split a string using regex pattern

How split a [0] like words from string using regex pattern.0 can replace any integer number.
I used regex pattern,
private static final String REGEX = "[\\d]";
But it returns string with [.
Spliting Code
Pattern p=Pattern.compile(REGEX);
String items[] = p.split(lure_value_save[0]);
You have to escape the brackets:
String REGEX = "\\[\\d+\\]";
Java doesn't offer an elegant solution to extract the numbers. This is the way to go:
Pattern p = Pattern.compile(REGEX);
String test = "[0],[1],[2]";
Matcher m = p.matcher(test);
List<String> matches = new ArrayList<String>();
while (m.find()) {
matches.add(m.group());
}

Categories

Resources