Get string inside parenthesis Java - java

I want get the value inside []. For example, this string:
String str ="[D][C][B][A]Hello world!";
and I want an array which contains item DCBA, how should I do this?
Thank you in advance!

Try with regex if there is only one character inside [].
Here Matcher#group() is used that groups any matches found inside parenthesis ().
Here escape character \ is used to escape the [ and ] that is already a part of regex pattern itself.
Sample code:
String str = "[D][C][B][A]Hello world!";
List<Character> list = new ArrayList<Character>();
Pattern p = Pattern.compile("\\[(.)\\]");
Matcher m = p.matcher(str);
while (m.find()) {
list.add(m.group(1).charAt(0));
}
Character[] array = list.toArray(new Character[list.size()]);
System.out.println(Arrays.toString(array));
Try this one if there is more than one character inside []
String str = "[DD][C][B][A]Hello world!";
List<String> list = new ArrayList<String>();
Pattern p = Pattern.compile("\\[(\\w*)\\]");
Matcher m = p.matcher(str);
while (m.find()) {
list.add(m.group(1));
}
String[] array = list.toArray(new String[list.size()]);
System.out.println(Arrays.toString(array));
Pattern description
\w A word character: [a-zA-Z_0-9]
. Any character (may or may not match line terminators)
X* X, zero or more times
Read more about here JAVA Regex Pattern

This code is simple
String str ="[D][C][B][A]Hello world!";
String[] s = str.split("\\[");
StringBuilder b = new StringBuilder();
for (String o: s) {
if (o.length() > 0){
String[] s2 = o.split("\\]");
b.append(s2[0]);
}
}
char[] c = b.toString().toCharArray();
System.out.println(new String(c));

Related

How can I find multiple words in Java regex

I want to check prohibition words.
In my codes,
public static String filterText(String sText) {
Pattern p = Pattern.compile("test", Pattern.CASE_INSENSITIVE);
Matcher m = p.matcher(sText);
StringBuffer buf = new StringBuffer();
while (m.find()){
m.appendReplacement(buf, maskWord(m.group()));
}
m.appendTail(buf);
return buf.toString();
}
public static String maskWord(String str) {
StringBuffer buf = new StringBuffer();
char[] ch = str.toCharArray();
for (int i = 0; i < ch.length; i++) {
buf.append("*");
}
return buf.toString();
}
If you receive the sentence "test is test", it will be expressed as "**** is ****" using the above code.
But I want to filter out at least a few tens to a few hundred words.
The words are stored in the DB.(DB Type: Oralce)
So how do I check multiple words?
Assuming you are using Java 9 you could use Matcher.replaceAll to replace the words in one statement. You can also use String.replaceAll to replace every character with '*'.
A pattern can contain many alternatives in it. You could construct a pattern with all the words required.
Pattern pattern = Pattern.compile("(word1|word2|word3)");
String result = pattern.matcher(input)
.replaceAll(w -> w.group(1).replaceAll(".", "*"));
Alternatively, you could have a list of patterns and then replace each in turn:
for (Pattern pattern: patternList)
result = pattern.matcher(result)
.replaceAll(w -> w.group(1).replaceAll(".", "*"));

How to trim a string using regex?

I have this alphabet: {'faa','fa','af'}
and I have this string: "faaf"
I have this regex: "(faa|fa|af)*" which helps me match the string with the alphabet.
How do I make Java trim my string into: {fa,af}, which is the correct way to write the string: "faaf" based on my alphabet?
here is my code:
String regex = "(faa|fa|af)*";
String str = "faaf";
boolean isMatch = Pattern.matches(regex, str);
if(isMatch)
{
//trim the string
while(str.length()!=0)
{
Pattern pattern = Pattern.compile("^(faa|fa|af)(faa|fa|af)*$");
Matcher mc = pattern.matcher(str);
if (mc.find())
{
String l =mc.group(1);
alphabet.add(l);
str = str.substring(l.length());
System.out.println("\n"+ l);
}
}
}
Thanks to Aaron who helped me with this problem.
You need a loop.
Pattern pattern = Pattern.compile(regex + "*");
LinkedList<String> parts = new LinkedList<>();
while (!str.isEmpty()) {
Matcher m = pattern.matcher(str);
if (!m.matches()) { // In the first loop step.
break;
}
parts.addFirst(m.group(1)); // The last repetition matching group.
str = str.substring(0, m.start(1));
}
String result = parts.stream().collect(Collectors.joining(", ", "{", "}"));
This utilizes that a match (X)+ will yield in m.group(1) the last occurrence's value of X.
Unfortunately the regex module does not provide a bored-open matches, such as the overloaded replaceAll with a lambda working on a single MatchResult.
Note that matches applies to the entire string.

Remove all the leading zero from the number part of a string

I am trying to remove all the leading zero from the number part of a string. I have came up with this code (below). From the given example it worked. But when I add a '0' in the begining it will not give the proper output. Anybody know how to achive this? Thanks in advance
input: (2016)abc00701def00019z -> output: (2016)abc701def19z -> resut: correct
input: 0(2016)abc00701def00019z -> output: (2016)abc71def19z -> result: wrong -> expected output: (2016)abc701def19z
EDIT: The string can contain other than english alphabet.
String localReference = "(2016)abc00701def00019z";
String localReference1 = localReference.replaceAll("[^0-9]+", " ");
List<String> lists = Arrays.asList(localReference1.trim().split(" "));
System.out.println(lists.toString());
String[] replacedString = new String[5];
String[] searchedString = new String[5];
int counter = 0;
for (String list : lists) {
String s = CharMatcher.is('0').trimLeadingFrom(list);
replacedString[counter] = s;
searchedString[counter++] = list;
System.out.println(String.format("Search: %s, replace: %s", list,s));
}
System.out.println(StringUtils.replaceEach(localReference, searchedString, replacedString));
str.replaceAll("(^|[^0-9])0+", "$1");
This removes any row of zeroes after non-digit characters and at the beginning of the string.
I tried doing the task using Regex and was able to do the required according to the two test cases you gave. Also $1 and $2 in the code below are the parts in the () brackets in preceding Regex.
Please find the code below:
public class Demo {
public static void main(String[] args) {
String str = "0(2016)abc00701def00019z";
/*Below line replaces all 0's which come after any a-z or A-Z and which have any number after them from 1-9. */
str = str.replaceAll("([a-zA-Z]+)0+([1-9]+)", "$1$2");
//Below line only replace the 0's coming in the start of the string
str = str.replaceAll("^0+","");
System.out.println(str);
}
}
java has \P{Alpha}+, which matches any non-alphabetic character and then removing the the starting Zero's.
String stringToSearch = "0(2016)abc00701def00019z";
Pattern p1 = Pattern.compile("\\P{Alpha}+");
Matcher m = p1.matcher(stringToSearch);
StringBuffer sb = new StringBuffer();
while(m.find()){
m.appendReplacement(sb,m.group().replaceAll("\\b0+",""));
}
m.appendTail(sb);
System.out.println(sb.toString());
output:
(2016)abc701def19z

How to split string using regex in java [duplicate]

This question already has an answer here:
Split regex to extract Strings of contiguous characters
(1 answer)
Closed 7 years ago.
I have some string patterns. Each pattern consist of two characters "A" and "B".
My patterns are like "AA" or "ABA" or "AABBABA" or ...
I want to split these patterns and the output for these examples must be like: {"AA"} or {"A","B","A"} or {"AA","BB","A","B","A"}
What I tried so far :
String pattern = "AABBABA" //or whatever
String firstChar = pattern.toString().substring(1, 2);
String[] split = pattern.split(firstChar);
for (String string : split) {
Log.i("findPattern", "Splitted Pattern: " + string + "");
}
The problem with my code is that it removes all strings that are equal to firstChar.
What regular expression should I use to split my patterns to separated strings?
The idea behind this is, (.)\\1+ helps to match any number of repeated characters at very first and this |. helps to match all the other single characters. Finally put all the matched characters into a list and then print it.
String s = "AABBABA";
ArrayList<String> fields = new ArrayList<String>();
Pattern regex = Pattern.compile("(.)\\1+|.");
Matcher m = regex.matcher(s);
while(m.find()){
fields.add(m.group(0));
}
System.out.println(fields);
}
Output:
[AA, BB, A, B, A]
By defining all the above input inside an array.
String s[] = {"AA", "ABA", "AABBABA"};
Pattern regex = Pattern.compile("(.)\\1+|.");
for(String i:s)
{
ArrayList<String> fields = new ArrayList<String>();
Matcher m = regex.matcher(i);
while(m.find()){
fields.add(m.group(0));
}
System.out.println(fields);
}
Output:
[AA]
[A, B, A]
[AA, BB, A, B, A]
I tried to follow your logics and got this code, perhaps, not what you want:
String pattern = "AABBABA"; //or whatever
String firstChar = pattern.toString().substring(1, 2);
String[] split = pattern.split("(?!" + firstChar + ")");
for (String strng : split)
{
System.console().writer().println(strng);
}
Output:
AA
B
BA
BA
Or try the Matcher:
// String to be scanned to find the pattern.
String line = "AABBABA";
String pattern1 = "(A+|B+)";
Pattern r = Pattern.compile(pattern1);
Matcher m = r.matcher(line);
int count = 0;
while(m.find())
{
count++;
System.console().writer().println(m.group(0));
}
Output:
AA
BB
A
B
A
you may capture what you want instead of split using this pattern
(A+|B+)
Demo

Working with a regular expression

I've a string with alpha numeric terms like below. I want to extract alphabets into an array. I've written following code.
String pro = "1a1a2aa3aaa4aaaa15aaaaa6aaaaaa";
String[] p = pro.split("^([0-9].*)$");
Pattern pattern = Pattern.compile("([0-9].*)([A-z].*)");
Matcher matcher = pattern.matcher(pro.toString());
while (matcher.find())
{
System.out.println(matcher.group());
}
for(String s: p)
{
System.out.println(s);
}
System.out.println("End");
Output:
1a1a2aa3aaa4aaaa15aaaaa6aaaaaa
ENd
I even tried to use split based on regular expression, but even that is not true. I think my regular expression is wrong. I'm expecting output with all the alphabets in array.
array[] = {'a', 'a', 'aa', 'aaa', 'aaaa', 'aaaaa', 'aaaaaa'}
You could use the following which split(s) on anything except alphabetic characters.
String s = "1a1a2aa3aaa4aaaa15aaaaa6aaaaaa";
String[] parts = s.split("[^a-zA-Z]+")
for (String m: parts) {
System.out.println(m);
}
Using the Matcher method, you could do the following.
String s = "1a1a2aa3aaa4aaaa15aaaaa6aaaaaa";
Pattern p = Pattern.compile("[a-zA-Z]+");
Matcher m = p.matcher(s);
List<String> matches = new ArrayList<String>();
while (m.find()) {
matches.add(m.group());
}
System.out.println(matches); // => [a, a, aa, aaa, aaaa, aaaaa, aaaaaa]
If you want only alphabet characters wouldn't make more sense to use this expression instead: /([a-zA-Z]+)/g
using ^ and $ is not something you may want in your expression because what you want instead is to match all possible matches /g
Here is an online demo:
http://regex101.com/r/fI1eB8

Categories