How get only characters between `(` and `)` using regular expression? - java

I am having a scenario like this:
A= add(1a,2b,3c,4d) now i want only the values inside the brackets .How can i do that ?Can anyone help me.
I tried using this:
replaceAll("\\d",""); It removed all the integers, but I want to get the char inside the bracket with commas.
For example: a,b,c,d

([0-9()]|.*(?=\())
[0-9()] This will match the digits and brackets
.*(?=\() This will match anything before the opening bracket
The | in the middle acts like an OR e.g. match (THIS | THIS)
In your case with a replace this A= add(1a,2b,3c,4d) will become a,b,c,d
Test here and choose the replace tab at the top http://gskinner.com/RegExr/

You can use this Regex:
\\((.*?)\\)
If you want to extract the chars without the ,, you can use String#split.
Your solution doesn't work because you are doing something irrelevant. You are replacing all digits with "", meaning that you're removing them.
Another solution:
String myStr = str.split("\\(|\\)")[1];
If your string is A= add(1a,2b,3c,4d), after the regex, you'll get 1a,2b,3c,4d. If you don't want the ints, use replaceAll.

The pattern you require is: "\\((.*)\\)".
The code below demonstrates how to use this pattern to find all the occurrences of items enclosed in brackets in an input string:
String example = "A= add(a,b,c,d)";
Pattern pattern = Pattern.compile("\\((.*)\\)");
Matcher matcher = pattern.matcher(example);
while (matcher.find()) {
System.out.println(matcher.group(1));
}
Prints:
a,b,c,d

\(([^\)]*)\) will give back in the first group "a,b,c,d".

Related

Split String 2 times but with different splits ";" and "."

Original String: "12312123;www.qwerty.com"
With this Model.getList().get(0).split(";")[1]
I get: "www.qwerty.com"
I tried doing this: Model.getList().get(0).split(";")[1].split(".")[1]
But it didnt work I get exception. How can I solve this?
I want only "qwerty"
Try this, to achieve "qwerty":
Model.getList().get(0).split(";")[1].split("\\.")[1]
You need escape dot symbol
Try to use split(";|\\.") like this:
for (String string : "12312123;www.qwerty.com".split(";|\\.")) {
System.out.println(string);
}
Output:
12312123
www
qwerty
com
You can split a string which has multiple delimiters. Example below:
String abc = "11;xyz.test.com";
String[] tokens = abc.split(";|\\.");
System.out.println(tokens[tokens.length-2]);
The array index 1 part doesn't make sense here. It will throw an ArrayIndexOutOfBounds Exception or something of the sort.
This is because splitting based on "." doesn't work the way you want it to. You would need to escape the period by putting "\." instead. You will find here that "." means something completely different.
You'd need to escape the ., i.e. "\\.". Period is a special character in regular expressions, meaning "any character".
What your current split means is "split on any character"; this means that it splits the string into a number of empty strings, since there is nothing between consecutive occurrences of " any character".
There is a subtle gotcha in the behaviour of the String.split method, which is that it discards trailing empty strings from the token array (unless you pass a negative number as the second parameter).
Since your entire token array consists of empty strings, all of these are discarded, so the result of the split is a zero-length array - hence the exception when you try to access one of its element.
Don't use split, use a regular expression (directly). It's safer, and faster.
String input = "12312123;www.qwerty.com";
String regex = "([^.;]+)\\.[^.;]+$";
Matcher m = Pattern.compile(regex).matcher(input);
if (m.find()) {
System.out.println(m.group(1)); // prints: qwerty
}

String split method returning first element as empty using regex

I'm trying to get the digits from the expression [1..1], using Java's split method. I'm using the regex expression ^\\[|\\.{2}|\\]$ inside split. But the split method returning me String array with first value as empty, and then "1" inside index 1 and 2 respectively. Could anyone please tell me what's wrong I'm doing in this regex expression, so that I only get the digits in the returned String array from split method?
You should use matching. Change your expression to:
`^\[(.*?)\.\.(.*)\]$`
And get your results from the two captured groups.
As for why split acts this way, it's simple: you asked it to split on the [ character, but there's still an "empty string" between the start of the string and the first [ character.
Your regex is matching [ and .. and ]. Thus it will split at this occurrences.
You should not use a split but match each number in your string using regex.
You've set it up such that [, ] and .. are delimiters. Split will return an empty first index because the first character in your string [1..1] is a delimiter. I would strip delimiters from the front and end of your string, as suggested here.
So, something like
input.replaceFirst("^[", "").split("^\\[|\\.{2}|\\]$");
Or, use regex and regex groups (such as the other answers in this question) more directly rather than through split.
Why not use a regex to capture the numbers? This will be more effective less error prone. In that case the regex looks like:
^\[(\d+)\.{2}(\d+)\]$
And you can capture them with:
Pattern pat = Pattern.compile("^\\[(\\d+)\\.{2}(\\d+)\\]$");
Matcher matcher = pattern.matcher(text);
if(matcher.find()) { //we've found a match
int range_from = Integer.parseInt(matcher.group(1));
int range_to = Integer.parseInt(matcher.group(2));
}
with range_from and range_to the integers you can no work with.
The advantage is that the pattern will fail on strings that make not much sense like ..3[4, etc.

Java Match string with optional hyphen

I am trying to match a series of string thats looks like this:
item1 = "some value"
item2 = "some value"
I have some strings, though, that look like this:
item-one = "some new value"
item-two = "some new value"
I am trying to parse it using regular expressions, but I can't get it to match the optional hyphen.
Here is my regex string:
Pattern p = Pattern.compile("^(\\w+[-]?)\\w+?\\s+=\\s+\"(.*)\"");
Matcher m = p.matcher(line);
m.find();
String option = m.group(1);
String value = m.group(2);
May someone please tell me what I could be doing wrong.
Thank you
I suspect that main reason of your problem is that you are expecting w+? to make w+ optional, where in reality it will make + quantifier reluctant so regex will still try to find at least one or more \\w here, consuming last character from ^(\\w+.
Maybe try this way
Pattern.compile("^(\\w+(?:-\\w+)?)\\s+=\\s+\"(.*?)\"");
in (\\w+(?:-\\w+)?) -> (?:-\\w+) part will create non-capturing group (regex wont count it as group so (.*?) will be group(2) even if this part will exist) and ? after it will make this part optional.
in \"(.*?)\" *? is reluctant quantifier which will make regex to look for minimal match that exist between quotation marks.
Demo
Your problem is that you have the ? in the wrong place:
Try this regex:
^((\\w+-)?\\w+)\\s*=\\s*\"([^\"]+)\"
But use groups 1 and 3.
I've cleaned up the regex a bit too
This regex should work for you:
^\w[\w-]*(?<=\w)\s*=\s*\"([^"]*)\"
In Java:
Pattern p = Pattern.compile("^\\w[\\w-]*(?<=\\w)\\s*=\\s*\"([^\"]*)\"");
Live Demo: http://www.rubular.com/r/0CvByDnj5H
You want something like this:
([\w\-]+)\s*=\s*"([^"]*)"
With extra backslashes for Java:
([\\w\\-]+)\\s*=\\s*\"([^\"]*)\"
If you expect other symbols to start appearing in the variable name, you could make it a character class like [^=\s] to accept any characters not = or whitespace, for example.

Java Repeat regular expression

I have following RegEx which should match e.g. some ids in brackets:
[swpf_02-7679, swpf_02-7622, ...]
Pattern p = Pattern.compile("[\\[\\s]*?[a-z]{1,8}[0-9]*?_[0-9]{2,}\\-[0-9]+[\\s]*?\\]");
The goal is now to combine this pattern with "split" at "," to fit the string [swpf_02-7679, swpf_02-7622] and not only [swpf_02-7679] like the posted RegEx above.
Can someone give me a hint?
Just remove the [ and ] from the string then split at the ,
The easiest way to do what you want to do I think is to just remove the '[' and ']' in front and back (use String.subString()), then split on comma with String.split() and use the regex on each individual string so returned (adjust the regex to remove the brackets of course).
Ok, assuming that you want the bits that the id's are like "swpf_02-7622", then split on the comma, and loop through the remains, trimming as you go. Some thing like
List<String> cleanIds = new ArrayList<String>();
for(String id : ids.split(","))
cleanIds.add(id.trim());
If you want rid of the "swpf_" bits, then id.substring(5).
Finally, to git rid of the square brackets, use id.startsWith('[') and id.endsWith(']') .
Why don't you use the Java StringTokenizer class and then just use the regex on the tokens you get out of this? You can post-process them to include the brackets you need or modify the regex slightly.
As #was and #garyh already mentioned the simplest way is to remove [], then split your list using `String.split("\s*,\S*"), then match each member using your pattern.
You can also match your string multiple times using start position as a end position of the previous iteration:
Pattern p = .... // your pattern in capturing brackets ()
Matcher m = p.matcher(str);
for (int start = 0; m.find(start); start = m.end()) {
String element = m.group(1);
// do what you need with the element.
}
If you simply want to extract all the codes in you list you could use this regular expression:
[^,\s\[\]]+
Getting all the matches from the following string:
[swpf_02-7679, swpf_02-762342, swpf_02-7633 , swpf_02-723422]
Would give you the following results:
swpf_02-7679
swpf_02-762342
swpf_02-7633
swpf_02-723422

Java: regex - how do i get the first quote text

As a beginner with regex i believe im about to ask something too simple but ill ask anyway hope it won't bother you helping me..
Lets say i have a text like "hello 'cool1' word! 'cool2'"
and i want to get the first quote's text (which is 'cool1' without the ')
what should be my pattern? and when using matcher, how do i guarantee it will remain the first quote and not the second?
(please suggest a solution only with regex.. )
Use this regular expression:
'([^']*)'
Use as follows: (ideone)
Pattern pattern = Pattern.compile("'([^']*)'");
Matcher matcher = pattern.matcher(s);
if (matcher.find()) {
System.out.println(matcher.group(1));
}
Or this if you know that there are no new-line characters in your quoted string:
'(.*?)'
when using matcher, how do i guarantee it will remain the first quote and not the second?
It will find the first quoted string first because it starts seaching from left to right. If you ask it for the next match it will give you the second quoted string.
If you want to find first quote's text without the ' you can/should use Lookahead and Lookbehind mechanism like
(?<=').*?(?=')
for example
System.out.println("hello 'cool1' word! 'cool2'".replaceFirst("(?<=').*?(?=')", "ABC"));
//out -> hello 'ABC' word! 'cool2'
more info
You could just split the string on quotes and get the second piece (which will be between the first and second quotes).
If you insist on regex, try this:
/^.*?'(.*?)'/
Make sure it's set to multiline, unless you know you'll never have newlines in your input. Then, get the subpattern from the result and that will be your string.
To support double quotes too:
/^.*?(['"])(.*?)\1/
Then get subpattern 2.

Categories