Java regex to obtain value between parenthesis - java

Regular expression to obtain value from [[text]]. I have tried the regex
"((?<=[[)*(?=]])*)+" the value between [[ ]] is not obtained.
For example, from the string [[text]], we should obtain text.
Pattern pat = Pattern.compile("((?<=\\[[)*(?=\\]])*)");
Matcher matcher = pat.matcher("[[text]]");
String next ="";
while(matcher.find()) {
next = matcher.group(0);
break;
}
System.out.println(next); //next should be text

You need to escape brackets [] when using them as actual characters in a regular expression. And you also need to add something to actually capture what is between the brackets. You can use .* for that or use my approach, if you are sure the value cannot contain a ].
((?<=\[\[)([^\]]*)(?=\]\]))+
There is not even really a need to use lookbacks und lookaheads unless you explictly need to exempt those limiters from the match. This will work just as well:
\[\[([\]]*\]\]
And obviously when you put these into a String, you need to add additional \ to escape the \ for the String...they are just more readable this way.

If you don't wanna get into regex, String.replaceAll can also help you.
String s2 = s.replaceAll("\\[", "").replaceAll("\\]", "");

"(?<=\\[\\[)[^\\]]*"
this should work for you

Related

Regex to replace string with quotes

I need to replace quotes in following string.
String str = "This is 'test()ing' and test()ing'";
Final output should be "This is test()ing and test()ing'";
i.e replace only if it starts with 'test() and ends with '. Text in between remains the same.
This one is not working.
str = str.replaceAll("^('test())(.*)(')$", "test()$2");
Please suggest suitable regex for the same.
This regular expression will have the desired outcome:
str = str.replaceAll("'test\\(\\)(.*?)'", "test()$1");
with .*? the string is matched lazily and doesn't match to the end of the whole string.
You can use:
str = str.replaceAll("'(test\\(\\)[^']*)'", "$1");
RegEx Demo
Doesn't look like you actually want to have the ^ and $ anchors (start/end of line) in your regex.
Aside from that, it will consume more than you want. If you want the .* to stop at the earliest point it can continue, you should make it match reluctantly, like .*?.
So your regex would be:
('test\(\))(.*?)(')
Make the middle part non greedy:
(')(.*?)(')
i.e.
str = str.replaceAll("(')(.*?)(')", "$2");
and if it should always start with test(), add
str = str.replaceAll("(')(test().*?)(')", "$2");
Check this example and this one.

java regular expression [A-Z]{6}-[A-Z]{4}-[A-Z]{4}

I'm trying to write a regular expression in Java for this:
"/[A-Z]{6}-[A-Z]{4}-[A-Z]{4}/"
But it is not working. For example
"AASAAA-AAAA-AAAA".matches("/[A-Z]{6}-[A-Z]{4}-[A-Z]{4}/")
returns false.
What is the correct way?
Java != JavaScript, here you don't need to surround regex with / so try with
"AASAAA-AAAA-AAAA".matches("[A-Z]{6}-[A-Z]{4}-[A-Z]{4}")
Otherwise your regex would search for substring which also has / at start and end.
BTW you need to know that matches checks if regex matches entire String, so
"aaa".matches("aa")
is same as
"aaa".matches("^aa$")
which would return false since String couldn't be fully matched by regex.
If you would like to find substrings which would match regex you would need to use
String input = "abcd";
Pattern regex = Pattern.compile("\\w{2}");
Matcher matcher = regex.matcher(input);
while (matcher.find()){//this will try to find single match
System.out.println(matcher.group());
}
Output:
ab
cd
^[A-Z]{6}-[A-Z]{4}-[A-Z]{4}$
It's just you shouldn't put backslashes at the start and the end. Put instead ^ and $.
And ow I didn't see you have used the Javscript's syntax ! Java != Javascript

How to prohibit a backslash with regex in java?

I'm new with regex and just can't find what the regex is to prohibit a backslash.
Thanks for any advice.
EDIT:
I'm using the regex for JTextField, to avoid that the user writes an unvalid input. This regex currently doesnt allow the user to write a space character.
I'm doing this with
String regex = "\\S{1}";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(str);
So how I change my regex to prohibit backslash as well?
Based on your example snippet, the following expression should work similarly, but also disallowing the backslash:
String regex = "[^\\s\\\\]{1}";
It is a bit strange that you are looking for a one-char non-space and non-backslash pattern, but I guess you are iterating through and checking for consecutive matches.
I would use the following regex though:
String regex = "[^\\s\\\\]+";
and check whether it matched the whole String (matcher.matches()).
The regex pattern with java is \\\\
String somestring;
somestring = somestring.replaceAll("\\\\", "");
Would remove them. Semantically it equates down to \\ at the regex level, which becomes a literal \ match.
You can also use a Pattern match if you want to just compare, or just use String#contains
String somestring;
if (somestring.contains("\\")) {...}
To test if a string has a backslash:
if (input.matches(".*\\\\.*"))
To remove backslashes from strings:
input = input.replace("\\", "");
Note the use of the non-regex based replace() method.

Java String Split on any character (including regex special characters)

I'm sure I'm just overlooking something here...
Is there a simple way to split a String on an explicit character without applying RegEx rules?
For instance, I receive a string with a dynamic delimiter, I know the 5th character defines the delimiter.
String s = "This,is,a,sample";
For this, it's simple to do
String delimiter = String.valueOf(s.charAt(4));
String[] result = s.split(delimiter);
However, when I have a delimiter that's a special RegEx character, this doesn't work:
String s = "This*is*a*sample";
So... is there a way to split the string on an explicit character without trying to apply extra RegEx rules? I feel like I must be missing something pretty simple.
split uses a regular expression as its argument. * is a meta-character used to match zero of more characters in regular expressions, You could use Pattern#quote to avoid interpreting the character
String[] result = s.split(Pattern.quote(delimiter));
You need not to worry about the character type If you use Pattern
Pattern regex = Pattern.compile(s.charAt(4));
Matcher matcher = regex.matcher(yourString);
if (matcher.find()){
//do something
}
You can run Pattern.quote on the delimiter before feeding it in. This will create a string literal and escape any regex specific chars:
delimiter = Pattern.quote(delimiter);
StringUtils.split(s, delimiter);
That will treat the delimiter as just a character, not use it like a regex.
StringUtils is a part of the ApacheCommons library, which is tons of useful methods. It is worth taking a look, could save you some time in the future.
Simply put your delimiter between []
String delimiter = "["+s.charAt(4)+"]";
String[] result = s.split(delimiter);
Since [ ] is the regex matches any characters between [ ]. You can also specify a list of delimiters like [*,.+-]

Java Regex to remove start/end single quotes but leave inside quotes

I have data from a CSV file that is enclosed in single quotes, like:
'Company name'
'Price: $43.50'
'New York, New York'
I want to be able to replace the single quotes at the start/end of the value but leave quotes in the data, like:
'Joe's Diner' should become Joe's Diner
I can do
updateString = theString.replace("^'", "").replace("'$", "");
but I wanted to know if I could combine it to only do one replace.
You could use the or operator.
updateString = theString.replaceAll("(^')|('$)","");
See if that works for you :)
updateString = theString.replaceFirst("^'(.*)'$", "$1");
Note that the form you have no won't work because replace uses literal strings, not regexes.
This works by using a capturing group (.*), which is referred to with $1 in the replacement text. You could also do something like:
Pattern patt = Pattern.compile("^'(.*)'$"); // could be stored in a static final field.
Matcher matcher = patt.matcher(theString);
boolean matches = matcher.matches();
updateString = matcher.group(1);
Of course, if you're certain there's a single quote at the beginning and end, the simplest solution is:
updateString = theString.substring(1, theString.length() - 1);
You can use regex to remove double quotes around digits/numbers.
jsonString.replaceAll("\"(\\d+)\"","$1");
above will not work if negative numbers are present.
for negative numbers, the regex will be a little complex like below. But I haven't tried it.
"([0-9]+\.{0,1}[0-9]*)"

Categories

Resources