Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 6 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
For a string foo, and a regular expression REGEX, why doesn't this work:
foo.indexOf(foo.matches(REGEX) ? '<' : "--");
what is wrong with this ternary?
The two expressions of the ternary conditional have to be the same type (or implicitly convertible to a common type). '<' and "--" are respectively a char and a java.lang.String. One cannot be implicitly converted into the other.
To fix, use the more long-winded
foo.matches(REGEX) ? foo.indexOf('<') : foo.indexOf("--");
Two different overloads of indexOf are used here but since the return types are identical the ternary conditional is grammatically correct.
Try changing this '<' to "<"
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I'm trying to define a Constant with some pattern identifier to be able to replace it in large String constructs later, for example in a complex Querie.
private static String SUBSTITUTE_1 = "${substitute1}";
But when I do that, compiler complains about expecting a number next to the '{' char.
My question is: Is there a way to escape the special characters in the string?
private static String SUBSTITUTE_1 = "\$\{substitute1\}";
This does not work.
This is strange because if I define a String builder like so
private static String SUBSTITUTE_1 = new StringBuilder("${substitute1}").toString();
there no problem with the special characters.
Unless there is something really obvious that i'm missing this does not make sense.
Thanks in advance.
Apparently, Intellij's compiler detects that somewhere in the code I'm using the defined constant in a String.replaceAll() instruction and complains about its definition as "${x}" is not a valid Regex Expression.
Solution is change to String.replace().
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I want to match a string containing slashes in Java. I have the following code:
String exclude = "some/class/in/package/*.class";
String className = "some/class/in/package/TheClass.class";
boolean value = className.matches(exclude);
System.out.println(value);
>false
Can anyone help me to fix this?
Valid class names must start with a letter so it could reasonably be
"some/class/in/package/[A-Za-z].*\\.class"
But then you probably ought to put in valid characters for the rest
"some/class/in/package/[A-Za-z][A-Za-z0-9_]*\\.class"
Nevermind, I had forgotten the '.' before '*'
String exclude = "some/class/in/package/.*.class";
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I have a string //*[#id=\"yui-main\"]/div/form/table/tbody/tr[1]/td[1] in a for loop.
I need to replace td[1] to td[i+1] .
Tried many regex expressions .
(?<=td\[)1(?=\])
Try this.Replace by i+1.See demo.
https://www.regex101.com/r/rG7gX4/5
This uses lookbehind to make sure 1 begin captured has td[ behind it and a lookahead to ensure it has ] ahead of it.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm looking for a java regex pattern that exclude any text that have two or more consecutive dashes or two or more consecutive spaces.
Assuming that you will use your regex in mechanism similar to matches method you may be looking for something like
^(?!.*(--| )).*$
try this
s = s.replaceAll("( )+|(-)+", "$1$2");
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Regex [abcd] can take any one character out of a, b, c and d.
What if I want to take any one string out of abc, def and ijk.
So something like ["abcd" "def" "efg"] (but this obviously doesn't work).
How would I do this in Java?
your regex could look like this:
(abcd|def|efg)
[] is only for single characters.
You can use | for multiple characters (X|Y means "Either X or Y"):
abcd|def|efg
In case there's anything else in the regex, you'd want to surround it with brackets:
other(abcd|def|efg)stuff
The above matches these strings:
otherabcdstuff
otherdefstuff
otherefgstuff
Where-as:
otherabcd|def|efgstuff
would obviously match these strings:
otherabcd
def
efgstuff