Finding the sub string - java

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");

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());
}

How to replace string with regex in Java or Clojure

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);

Regex after a special character in Java

I am using regex in java to get a specific output from a list of rooms at my University.
A outtake from the list looks like this:
(A55:G260) Laboratorium 260
(A55:G292) Grupperom 292
(A55:G316) Grupperom 316
(A55:G366) Grupperom 366
(HDS:FLØYEN) Fløyen (appendix)
(ODO:PC-STUE) Pulpakammeret (PC-stue)
(SALEM:KONF) Konferanserom
I want to get the value that comes between the colon and the parenthesis.
The regex I am using at the moment is:
pattern = Pattern.compile("[:]([A-Za-z0-9ÆØÅæøå-]+)");
matcher = pattern.matcher(room.text());
I've included ÆØÅ, because some of the rooms have Norwegian letters in them.
Unfortunately the regex includes the building code also (e.g. "A55") in the output... Comes out like this:
A55
A55
A55
:G260
:G292
:G316
Any ideas on how to solve this?
The problem is not your regular expression. You need to reference group(1) for the match result.
while (matcher.find()) {
System.out.println(matcher.group(1));
}
However, you may consider using a negated character class instead.
pattern = Pattern.compile(":([^)]+)");
You can try a regex like this :
public static void main(String[] args) {
String s = "(HDS:FLØYEN) Fløyen (appendix)";
// select everything after ":" upto the first ")" and replace the entire regex with the selcted data
System.out.println(s.replaceAll(".*?:(.*?)\\).*", "$1"));
String s1 = "ODO:PC-STUE) Pulpakammeret (PC-stue)";
System.out.println(s1.replaceAll(".*?:(.*?)\\).*", "$1"));
}
O/P :
FLØYEN
PC-STUE
Can try with String Opreations as follows,
String val = "(HDS:FLØYEN) Fløyen (appendix)";
if(val.contains(":")){
String valSub = val.split("\\s")[0];
System.out.println(valSub);
valSub = valSub.substring(1, valSub.length()-1);
String valA = valSub.split(":")[0];
String valB = valSub.split(":")[1];
System.out.println(valA);
System.out.println(valB);
}
Output :
(HDS:FLØYEN)
HDS
FLØYEN
import java.util.regex.Matcher;
import java.util.regex.Pattern;
class test
{
public static void main( String args[] ){
// String to be scanned to find the pattern.
String line = "(HDS:FLØYEN) Fløyen (appendix)";
String pattern = ":([^)]+)";
// Create a Pattern object
Pattern r = Pattern.compile(pattern);
// Now create matcher object.
Matcher m = r.matcher(line);
while (m.find()) {
System.out.println(m.group(1));
}
}
}

First and second tocen regex

How could I get the first and the second text in "" from the string?
I could do it with indexOf but this is really boring ((
For example I have a String for parse like: "aaa":"bbbbb"perhapsSomeOtherText
And I d like to get aaa and bbbbb with the help of Regex pattern - this will help me to use it in switch statement and will greatly simplify my app/
If all that you have is colon delimited string just split it:
String str = ...; // colon delimited
String[] parts = str.split(":");
Note, that split() receives regex and compilies it every time. To improve performance of your code you can use Pattern as following:
private static Pattern pColonSplitter = Pattern.compile(":");
// now somewhere in your code:
String[] parts = pColonSplitter.split(str);
If however you want to use pattern for matching and extraction of string fragments in more complicated cases, do it like following:
Pattert p = Patter.compile("(\\w+):(\\w+):");
Matcher m = p.matcher(str);
if (m.find()) {
String a = m.group(1);
String b = m.group(2);
}
Pay attention on brackets that define captured group.
Something like this?
Pattern pattern = Pattern.compile("\"([^\"]*)\"");
Matcher matcher = pattern.matcher("\"aaa\":\"bbbbb\"perhapsSomeOtherText");
while (matcher.find()) {
System.out.println(matcher.group(1));
}
Output
aaa
bbbbb
String str = "\"aaa\":\"bbbbb\"perhapsSomeOtherText";
Pattern p = Pattern.compile("\"\\w+\""); // word between ""
Matcher m = p.matcher(str);
while(m.find()){
System.out.println(m.group().replace("\"", ""));
}
output:
aaa
bbbbb
there are several ways to do this
Use StringTokenizer or Scanner with UseDelimiter method

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