String split question using "*" - java

Let's say have a string...
String myString = "my*big*string*needs*parsing";
All I want is to get an split the string into "my" , "big" , "string", etc.
So I try
myString.split("*");
returns
java.util.regex.PatternSyntaxException: Dangling meta character '*' near index 0
* is a special character in regex so I try escaping....
myString.split("\\*");
same exception. I figured someone would know a quick solution. Thanks.

split("\\*") works with me.

One escape \ will not do the trick in Java 6 on Mac OSX, as \ is reserved for \b \t \n \f \r \'\" and \\. What you have seems to work for me:
public static void main(String[] args) {
String myString = "my*big*string*needs*parsing";
String[] a = myString.split("\\*");
for (String b : a) {
System.out.println(b);
}
}
outputs:
my big string needs parsing

http://arunma.com/2007/08/23/javautilregexpatternsyntaxexception-dangling-meta-character-near-index-0/
Should do exactly what you need.

myString.split("\\*"); is working fine on Java 5. Which JRE do you use.

You can also use a StringTokenizer.
StringTokenizer st = new StringTokenizer("my*big*string*needs*parsing", "\*");
while (st.hasMoreTokens()) {
System.out.println(st.nextToken());
}

This happens because the split method takes a regular expression, not a plain string.
The '*' character means match the previous character zero or more times, thus it is not valid to specify it on its own.
So it should be escaped, like following
split("\\*")

Related

How to remove all non-alphabetic character excluding space using meta character from a string?

Is there a way to achieve this using meta character?
I can do this by the following regular expression : (note the space in between)
s.replaceAll("[^A-Z a-z]","")
But how to do this using meta character as I can't implement AND.
Following code obviously doesn't work , but how to do a similar thing?
s.replaceAll("\\S&\\d|\\W, "")
You can do as follows:
s = s.replaceAll("[^a-zA-Z\\s]", "");
To keep number too, you can do:
s = s.replaceAll("[^a-zA-Z0-9\\s]", "");
You can use the regex, [^A-Za-z\\s].
Demo:
public class Main {
public static void main(String[] args) {
String s = "123 & Hello * World!";
s = s.replaceAll("[^A-Za-z\\s]", "");
System.out.println(s);
}
}
Output:
Hello World
[^A-Z a-z] is called a character class (negated here).
\S, \w, etc. are called shorthand character classes.
You ask if you can match any character different from a letter and space with a shorthand character class.
The answer is: there is no such a shorthand class, but there are alternatives:
[^\\p{Alpha} ]
[^\\p{Alpha}\\p{javaSpaceChar}]
[\\p{N}\\p{P}\\p{S}]

How can I find all "((" and replace them with "("?

I'm given a string, and I want to replace all open parenthesis that occur in succession, with a single one
((5)) → (5)
((((5)))) → (5)
I tried
str = str.replaceAll("((", "(");
and got regex patttern error
then i tried
str = str.replaceAll("\\((", "(");
then i tried
str = str.replaceAll("\\\\((", "(");
I keep getting the same error!
have you tried this?
str = str.replaceAll("\\({2,}", "(");
The '\' is the escape character, so every special character must be proceeded by it. Without them, regex reads it as an open parentheses used for grouping and expects a closed parentheses.
Edit: Originally, I thought he was trying to match exactly 2
You need to escape each parenthesis and add + to account for successive occurrences:
str = str.replaceAll("\\(\\(+","(");
Assuming the parentheses don't need to be paired, e.g. ((((5)) should become (5), then the following will do:
str = str.replaceAll("([()])\\1+", "$1");
Test
for (String str : new String[] { "(5)", "((5))", "((((5))))", "((((5))" }) {
str = str.replaceAll("([()])\\1+", "$1");
System.out.println(str);
}
Output
(5)
(5)
(5)
(5)
Explanation
( Start capture group
[()] Match a '(' or a ')'. In a character class, '(' and ')'
has no special meaning, so they don't need to be escaped
) End capture group, i.e. capture the matched '(' or ')'
\1+ Match 1 or more of the text from capture group #1. As a
Java string literal, the `\` was escaped (doubled)
$1 Replace with the text from capture group #1
See also regex101.com for demo.
I am not sure if the brackets are fixed or dynamic but assuming they may be dynamic what you could do here is use replaceAll and then use String.Format to format the string.
Hope it helps
public class HelloWorld{
public static void main(String []args){
String str = "((((5))))";
String abc = str.replaceAll("\\(", "").replaceAll("\\)","");
abc = String.format("(%s)", abc);
System.out.println(abc);
}
}
Output: (5)
I have tried the above code with ((5)) and (((5))) and it produces the same output.

Insert character before specific character Java

I have been taking a look at the regular expressions and how to use it in Java for the problem I have to solve. I have to insert a \ before every ". This is what I have:
public class TestExpressions {
public static void main (String args[]) {
String test = "$('a:contains(\"CRUCERO\")')";
test = test.replaceAll("(\")","$1%");
System.out.println(test);
}
}
The ouput is:
$('a:contains("%CRUCERO"%)')
What I want is:
$('a:contains(\"CRUCERO\")')
I have changed % for \\ but have an error StringIndexOutofBounds don't know why. If someone can help me I would appreciate it, thank you in advance.
I have to insert a \ before every "
You can try with replace which automatically escapes all regex metacharacters and doesn't use any special characters in replacement part so you can simply use String literals you want to be put in matched part.
So lets just replace " with \" literal. You can write it as
test = test.replace("\"", "\\\"");
If you want to insert backspace before quote then use:
test = test.replaceAll("(\")","\\\\$1"); // $('a:contains(\"CRUCERO\")')
Or if you want to avoid already escaped quote then use negative lookbehind:
String test = "$('a:contains(\\\"CRUCERO\")')";
test = test.replaceAll("((?<!\\\\)\")","\\\\$1"); // $('a:contains(\"CRUCERO\")')
String result = subject.replaceAll("(?i)\"CRUCERO\"", "\\\"CRUCERO\\\"");
EXPLANATION:
Match the character string “"CRUCERO"” literally (case insensitive) «"CRUCERO"»
Ignore unescaped backslash «\»
Insert the character string “"CRUCERO” literally «"CRUCERO»
Ignore unescaped backslash «\»
Insert the character “"” literally «"»
If your goal is escape text for Java strings, then instead of regular expressions, consider using
String escaped = org.apache.commons.lang.StringEscapeUtils.
escapeJava("$('a:contains(\"CRUCERO\")')");
System.out.println(escaped);
Output:
$('a:contains(\"CRUCERO\")')
JavaDoc: http://commons.apache.org/proper/commons-lang/javadocs/api-2.6/org/apache/commons/lang/StringEscapeUtils.html#escapeJava(java.lang.String)

how to split the string and store it in array

I have tried the following example but it gives following out put
output[]. I have pass the string "1.0" to function calculatePayout() and want to store the 1 in s[0] and 0 in s[1]
import java.util.Arrays;
public class aps {
public void calculatePayout(String amount)
{
String[] s = amount.split(".");
System.out.println("output"+Arrays.toString(s));
}
public static void main(String args[])
{
new aps().calculatePayout("1.0");
}
}
Method split() accepts regular expression. Character . in regular expressions means "everything". To split your string with . you have to escape it, i.e. split("\\."). The second back slash is needed because the first one escapes dot for regular expression, the second escapes back slash for java compiler.
Try escaping the dot:
String[] s = amount.split("\\.");
As dot is "Any character" in regex.
Try amount.split("\\.")
Split method uses a regex so you need to use the correct syntax and escape the dot.
. is a metacharcter or special character in regex world. String#split(regex) expects regex as parameter, you either have to escape it with backslash or use character class in-order to treat it as a normal character
Either amount.split("\\."); or amount.split("[.]");
Try
amount.split("\\.")
split method accepts a regular expression

Regular expression to match unescaped special characters only

I'm trying to come up with a regular expression that can match only characters not preceded by a special escape sequence in a string.
For instance, in the string Is ? stranded//? , I want to be able to replace the ? which hasn't been escaped with another string, so I can have this result : **Is Dave stranded?**
But for the life of me I have not been able to figure out a way. I have only come up with regular expressions that eat all the replaceable characters.
How do you construct a regular expression that matches only characters not preceded by an escape sequence?
Use a negative lookbehind, it's what they were designed to do!
(?<!//)[?]
To break it down:
(
?<! #The negative look behind. It will check that the following slashes do not exist.
// #The slashes you are trying to avoid.
)
[\?] #Your special charactor list.
Only if the // cannot be found, it will progress with the rest of the search.
I think in Java it will need to be escaped again as a string something like:
Pattern p = Pattern.compile("(?<!//)[\\?]");
Try this Java code:
str="Is ? stranded//?";
Pattern p = Pattern.compile("(?<!//)([?])");
m = p.matcher(str);
StringBuffer sb = new StringBuffer();
while (m.find()) {
m.appendReplacement(sb, m.group(1).replace("?", "Dave"));
}
m.appendTail(sb);
String s = sb.toString().replace("//", "");
System.out.println("Output: " + s);
OUTPUT
Output: Is Dave stranded?
I was thinking about this and have a second simplier solution, avoiding regexs. The other answers are probably better but I thought I might post it anyway.
String input = "Is ? stranded//?";
String output = input
.replace("//?", "a717efbc-84a9-46bf-b1be-8a9fb714fce8")
.replace("?", "Dave")
.replace("a717efbc-84a9-46bf-b1be-8a9fb714fce8", "?");
Just protect the "//?" by replacing it with something unique (like a guid). Then you know any remaining question marks are fair game.
Use grouping. Here's one example:
import java.util.regex.*;
class Test {
public static void main(String[] args) {
Pattern p = Pattern.compile("([^/][^/])(\\?)");
String s = "Is ? stranded//?";
Matcher m = p.matcher(s);
if (m.matches)
s = m.replaceAll("$1XXX").replace("//", "");
System.out.println(s + " -> " + s);
}
}
Output:
$ java Test
Is ? stranded//? -> Is XXX stranded?
In this example, I'm:
first replacing any non-escaped ? with "XXX",
then, removing the "//" escape sequences.
EDIT Use if (m.matches) to ensure that you handle non-matching strings properly.
This is just a quick-and-dirty example. You need to flesh it out, obviously, to make it more robust. But it gets the general idea across.
Match on a set of characters OTHER than an escape sequence, then a regex special character. You could use an inverted character class ([^/]) for the first bit. Special case an unescaped regex character at the front of the string.
String aString = "Is ? stranded//?";
String regex = "(?<!//)[^a-z^A-Z^\\s^/]";
System.out.println(aString.replaceAll(regex, "Dave"));
The part of the regular expression [^a-z^A-Z^\\s^/] matches non-alphanumeric, whitespace or non-forward slash charaters.
The (?<!//) part does a negative lookbehind - see docco here for more info
This gives the output Is Dave stranded//?
try matching:
(^|(^.)|(.[^/])|([^/].))[special characters list]
I used this one:
((?:^|[^\\])(?:\\\\)*[ESCAPABLE CHARACTERS HERE])
Demo: https://regex101.com/r/zH1zO3/4

Categories

Resources