gudii9
asked on
xyBalance chalenge
Hi,
I am working on below challenge
http://codingbat.com/prob/p134250
I tried my code as below
how to improve my approach, results and design of this challenge. How do i make a graphical venn or flow chart or some other relevant diagram to design it before writing code to decide best strategy?
Please advise
I am working on below challenge
http://codingbat.com/prob/p134250
I tried my code as below
public boolean xyBalance(String str) {
int xPos= str.indexOf('x');
int yPos= str.indexOf('y');
if(xPos>=0&&yPos>=0){
return true;
}
else{
return false;
}
}
I am getting below resultExpected Run
xyBalance("aaxbby") → true true OK
xyBalance("aaxbb") → false false OK
xyBalance("yaaxbb") → false true X
xyBalance("yaaxbby") → true true OK
xyBalance("xaxxbby") → true true OK
xyBalance("xaxxbbyx") → false true X
xyBalance("xxbxy") → true true OK
xyBalance("xxbx") → false false OK
xyBalance("bbb") → true false X
xyBalance("bxbb") → false false OK
xyBalance("bxyb") → true true OK
xyBalance("xy") → true true OK
xyBalance("y") → true false X
xyBalance("x") → false false OK
xyBalance("") → true false X
xyBalance("yxyxyxyx") → false true X
xyBalance("yxyxyxyxy") → true true OK
xyBalance("12xabxxydxyxyzz") → true
how to improve my approach, results and design of this challenge. How do i make a graphical venn or flow chart or some other relevant diagram to design it before writing code to decide best strategy?
Please advise
return !str.matches(".*x[^y]*");
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
We'll say that a String is xy-balanced if for all the 'x' chars in the string, there exists a 'y' char somewhere later in the string. So "xxy" is balanced, but "xyx" is not.
i am not clear on this challenge.
So "xxy" is balanced, but "xyx" is not
how xxy is balance there are 2 x's and only 1 y's right and 2 is not equal to 1 so not balanced right?
so total number of X's should be equal to total number of Y's then balanced is true accoring to challenge???
not sure why below is false?( as number of x's is one and number of y's is1 which seems balanced to me??)
xyBalance("yaaxbb") → false
I think it was a poor choice of words to use "balanced" to define the challenge criteria. The number of x's and y's has nothing to do with the string being balanced. It's considered to be balanced when each and every x is later followed by a y. So xxxxxxxxxy would be balanced and xyyyyyyyyx would not.
With "yaaxbb" there is no y after the x and with "xyx" there is no y after all x's.
With "yaaxbb" there is no y after the x and with "xyx" there is no y after all x's.
ASKER
It's considered to be balanced when each and every x is later followed by a y. So xxxxxxxxxy would be balancedxxxxxxxxxy
above highlighted x does not followed by y(instead it is followed by another x as italicized and underlined??
You're not understanding how the challenge defines the criteria for being balanced. It states,
String is xy-balanced if for all the 'x' chars in the string, there exists a 'y' char somewhere later in the string
and One 'y' can balance multiple 'x's
So with xxxxxxxxxy , the first x is followed later by a y, the second x is followed later by a y (happens to be the same y), the third x ....etc. All of the x's are followed later by the same y.
String is xy-balanced if for all the 'x' chars in the string, there exists a 'y' char somewhere later in the string
and One 'y' can balance multiple 'x's
So with xxxxxxxxxy , the first x is followed later by a y, the second x is followed later by a y (happens to be the same y), the third x ....etc. All of the x's are followed later by the same y.
http:#a41439798
Instead:
return str.indexOf("x") < 0 || str.lastIndexOf("y") > str.lastIndexOf("x");
or
return str.lastIndexOf("y") >= str.lastIndexOf("x");
return str.indexOf("x") > 0 && str.lastIndexOf("y") > str.lastIndexOf("x");Close.
Instead:
return str.indexOf("x") < 0 || str.lastIndexOf("y") > str.lastIndexOf("x");
or
return str.lastIndexOf("y") >= str.lastIndexOf("x");
ASKER
I got meaning of challenge
ASKER
public boolean xyBalance(String str) {
for(int i=0;i<str.length();i++){
int xPos= str.indexOf('x');
int yPos= str.indexOf('y');
int len=str.length();
if(xPos>=0&&yPos>=0&&(yPos>xPos)){
return true;
}
}
return false;
}
above fails below.
Expected Run
xyBalance("aaxbby") → true true OK
xyBalance("aaxbb") → false false OK
xyBalance("yaaxbb") → false false OK
xyBalance("yaaxbby") → true false X
xyBalance("xaxxbby") → true true OK
xyBalance("xaxxbbyx") → false true X
xyBalance("xxbxy") → true true OK
xyBalance("xxbx") → false false OK
xyBalance("bbb") → true false X
xyBalance("bxbb") → false false OK
xyBalance("bxyb") → true true OK
xyBalance("xy") → true true OK
xyBalance("y") → true false X
xyBalance("x") → false false OK
xyBalance("") → true false X
xyBalance("yxyxyxyx") → false false OK
xyBalance("yxyxyxyxy") → true false X
xyBalance("12xabxxydxyxyzz") → true true OK
other tests
X
Correct for more than half the tests
please advise
for all the 'x' chars in the string, there exists a 'y' char somewhere later in the stringxyBalance("yaaxbby") → true false X
xyBalance("y") → true false X
xyBalance("yxyxyxyxy") → true false X
xyBalance("") → true false X
for all the 'x' characters, there exists a 'y' char somewhere later in the string
xyBalance("xaxxbbyx") → false true X
for this x character, there does not exist a 'y' char somewhere later in the string
ASKER
xyBalance("yaaxbby") → true false X
xyBalance("y") → true false X
xyBalance("yxyxyxyxy") → true false X
xyBalance("") → true false X
for all the 'x' characters, there exists a 'y' char somewhere later in the string
xyBalance("xaxxbbyx") → false true X
for this x character, there does not exist a 'y' char somewhere later in the string
i got the description of the challenge but i have not understood why my code approach is failing some tests?
public boolean xyBalance(String str) {
for(int i=0;i<str.length();i++){
int xPos= str.indexOf('x');
int yPos= str.indexOf('y');
int len=str.length();
if(xPos>=0&&yPos>=0&&(yPos>xPos)){
return true;
}
}
return false;
}
i am checking y should be always lter compared to x by getting index positions of them
if(xPos>=0&&yPos>=0&&(yPos
ASKER
as below
public boolean xyBalance(String str) {
for(int i=0;i<str.length();i++){
int xPos= str.indexOf('x');
int yPos= str.indexOf('y');
int len=str.length();
if(xPos>=0&&yPos>=0&&(yPos>xPos)){
return true;
}
}
return false;
}
Expected Run
xyBalance("aaxbby") → true true OK
xyBalance("aaxbb") → false false OK
xyBalance("yaaxbb") → false false OK
xyBalance("yaaxbby") → true false X
xyBalance("xaxxbby") → true true OK
xyBalance("xaxxbbyx") → false true X
xyBalance("xxbxy") → true true OK
xyBalance("xxbx") → false false OK
xyBalance("bbb") → true false X
xyBalance("bxbb") → false false OK
xyBalance("bxyb") → true true OK
xyBalance("xy") → true true OK
xyBalance("y") → true false X
xyBalance("x") → false false OK
xyBalance("") → true false X
xyBalance("yxyxyxyx") → false false OK
xyBalance("yxyxyxyxy") → true false X
xyBalance("12xabxxydxyxyzz") → true true OK
other tests
X
Correct for more than half the tests
ASKER
public class XyBalanace {
public static void main(String[] args) {
System.out.println("Values is--->"+xyBalance("yaaxbby"));
}
public static boolean xyBalance(String str) {
for(int i=0;i<str.length();i++){
int xPos= str.indexOf('x');
int yPos= str.indexOf('y');
int len=str.length();
if(xPos>=0&&yPos>=0&&(yPos>xPos)){
return true;
}
}
return false;
}
}
i expected above true not sure why false coming as below
Values is--->false
ASKER
public class XyBalanace {
public static void main(String[] args) {
System.out.println("Values is--->"+xyBalance("yaaxbby"));
}
public static boolean xyBalance(String str) {
for(int i=0;i<str.length()-1;i++){
int xPos= str.indexOf('x');
int yPos= str.indexOf('y');
int len=str.length();
if(xPos>=0&&yPos>=0&&(yPos>xPos)){
return true;
}
}
return false;
}
}
making str.length()-1 also did not help
int xPos= str.indexOf('x');xyBalance("yaaxbby") → true false X
int yPos= str.indexOf('y');
xPos is the index of this x
yPos is the index of this y
xyBalance("xaxxbbyx") → false true X
xPos is the index of this x
yPos is the index of this y
xyBalance("bbb") → true false X
xPos is -1
yPos is -1
xyBalance("y") → true false X
xPos is -1
yPos is the index of this y
xyBalance("") → true false X
xPos is -1
yPos is -1
xyBalance("yxyxyxyxy") → true false X
xPos is the index of this x
yPos is the index of this y
ASKER
public boolean xyBalance(String str) {
for(int i=0;i<str.length();i++){
int xPos= str.lastIndexOf('x');
int yPos= str.lastIndexOf('y');
int len=str.length();
if(xPos>=0&&yPos>=0&&(yPos>xPos)){
return true;
}
}
return false;
}
above failing few edge cases
Expected Run
xyBalance("aaxbby") → true true OK
xyBalance("aaxbb") → false false OK
xyBalance("yaaxbb") → false false OK
xyBalance("yaaxbby") → true true OK
xyBalance("xaxxbby") → true true OK
xyBalance("xaxxbbyx") → false false OK
xyBalance("xxbxy") → true true OK
xyBalance("xxbx") → false false OK
xyBalance("bbb") → true false X
xyBalance("bxbb") → false false OK
xyBalance("bxyb") → true true OK
xyBalance("xy") → true true OK
xyBalance("y") → true false X
xyBalance("x") → false false OK
xyBalance("") → true false X
xyBalance("yxyxyxyx") → false false OK
xyBalance("yxyxyxyxy") → true true OK
xyBalance("12xabxxydxyxyzz") → true true OK
other tests
OK
Correct for more than half the tests
Your progress graph for this problem
ASKER
xyBalance("bbb") → true false Xhow above true
xyBalance("y") → true false X
xyBalance("") → true false X
for all the 'x' chars in the string, there exists a 'y' char somewhere later in the string.
ASKER
but above cases there are no x chars and challenge does not talk about this scenario??
The challange talks about for all the 'x' chars this is universal quantification
https://en.wikipedia.org/wiki/Universal_quantification
in particular https://en.wikipedia.org/wiki/Universal_quantification#The_empty_set
there exists a 'y' char is Existential_quantification https://en.wikipedia.org/wiki/Existential_quantification
Note these equivalences http://www.cs.yale.edu/homes/aspnes/pinewiki/MathematicalLogic.html#Negation_and_quantifiers
https://en.wikipedia.org/wiki/Universal_quantification
in particular https://en.wikipedia.org/wiki/Universal_quantification#The_empty_set
there exists a 'y' char is Existential_quantification
Note these equivalences http://www.cs.yale.edu/homes/aspnes/pinewiki/MathematicalLogic.html#Negation_and_quantifiers
ASKER
We'll say that a String is xy-balanced if for all the 'x' chars in the string, there exists a 'y' char somewhere later in the string. So "xxy" is balanced, but "xyx" is not. One 'y' can balance multiple 'x's. Return true if the given string is xy-balanced.
so if x itself is not there then it is always true no matter y there or not which is obviously irrelevant right??
Yes, if x is not there, it is always true.
ASKER
public boolean xyBalance(String str) {
for(int i=0;i<str.length();i++){
int xPos= str.lastIndexOf('x');
int yPos= str.lastIndexOf('y');
int len=str.length();
if((xPos>=0&&yPos>=0&&(yPos>xPos))||(!str.contains("x")) || str.length()==0 ){
return true;
}
}
return false;
}
why above failing one as below?
Expected Run
xyBalance("aaxbby") → true true OK
xyBalance("aaxbb") → false false OK
xyBalance("yaaxbb") → false false OK
xyBalance("yaaxbby") → true true OK
xyBalance("xaxxbby") → true true OK
xyBalance("xaxxbbyx") → false false OK
xyBalance("xxbxy") → true true OK
xyBalance("xxbx") → false false OK
xyBalance("bbb") → true true OK
xyBalance("bxbb") → false false OK
xyBalance("bxyb") → true true OK
xyBalance("xy") → true true OK
xyBalance("y") → true true OK
xyBalance("x") → false false OK
xyBalance("") → true false X
xyBalance("yxyxyxyx") → false false OK
xyBalance("yxyxyxyxy") → true true OK
xyBalance("12xabxxydxyxyzz
other tests
OK
Correct for more than half the tests
ASKER
ok i have to say <= instead of <
public boolean xyBalance(String str) {
for(int i=0;i<=str.length();i++){
int xPos= str.lastIndexOf('x');
int yPos= str.lastIndexOf('y');
int len=str.length();
if((xPos>=0&&yPos>=0&&(yPo s>xPos))|| (!str.cont ains("x")) || str.length()==0 ){
return true;
}
}
return false;
}
which oassed all tests
output
Values is--->true
Values is--->0
public boolean xyBalance(String str) {
for(int i=0;i<=str.length();i++){
int xPos= str.lastIndexOf('x');
int yPos= str.lastIndexOf('y');
int len=str.length();
if((xPos>=0&&yPos>=0&&(yPo
return true;
}
}
return false;
}
which oassed all tests
Expected Run
xyBalance("aaxbby") → true true OK
xyBalance("aaxbb") → false false OK
xyBalance("yaaxbb") → false false OK
xyBalance("yaaxbby") → true true OK
xyBalance("xaxxbby") → true true OK
xyBalance("xaxxbbyx") → false false OK
xyBalance("xxbxy") → true true OK
xyBalance("xxbx") → false false OK
xyBalance("bbb") → true true OK
xyBalance("bxbb") → false false OK
xyBalance("bxyb") → true true OK
xyBalance("xy") → true true OK
xyBalance("y") → true true OK
xyBalance("x") → false false OK
xyBalance("") → true true OK
xyBalance("yxyxyxyx") → false false OK
xyBalance("yxyxyxyxy") → true true OK
xyBalance("12xabxxydxyxyzz") → true true OK
other tests
OK
public class XyBalanace {
public static void main(String[] args) {
System.out.println("Values is--->" + xyBalance(""));
String str="";
System.out.println("Values is--->" + str.length());
}
public static boolean xyBalance(String str) {
for (int i = 0; i <= str.length(); i++) {
int xPos = str.lastIndexOf('x');
int yPos = str.lastIndexOf('y');
int len = str.length();
if ((xPos >= 0 && yPos >= 0 && (yPos > xPos)) || (!str.contains("x")) || str.length() == 0) {
return true;
}
}
return false;
}
}
output
Values is--->true
Values is--->0
ASKER
but my approach is bit rough and lengthy loooks like
There is no need for the loop here.
ASKER
public boolean xyBalance(String str) {
//for(int i=0;i<=str.length();i++){
int xPos= str.lastIndexOf('x');
int yPos= str.lastIndexOf('y');
int len=str.length();
if((xPos>=0&&yPos>=0&&(yPos>xPos))|| (!str.cont ains("x")) || str.length()==0 ){
return true;
}
//}
return false;
}
i see without loop also i passed all tests?
when to decide to put loop when not?
A loop can be used to apply the same operation on different operands.
When the operands are always the same, the loop is probably not useful.
When the operands are always the same, the loop is probably not useful.
lastIndexOf, length(), contains, >=, >, &&, ||, !, ==
were all operations in the loop with operands that did not change, so they can be taken out of the loop
all together
were all operations in the loop with operands that did not change, so they can be taken out of the loop
all together
{
int xPos = str.lastIndexOf('x');
int yPos = str.lastIndexOf('y');
int len = str.length();
if ((xPos >= 0 && yPos >= 0 && (yPos > xPos)) || (!str.contains("x")) || str.length() == 0) {
return true;
}
}
can be considered an operation
(xPos >= 0 && yPos >= 0 && (yPos > xPos)) || (!str.contains("x")) || str.length() == 0
could also have been simplified
could also have been simplified
ASKER
(xPos >= 0 && yPos >= 0 && (yPos > xPos)) || (!str.contains("x")) || str.length() == 0
could also have been simplified
just to be 100% clear on below
lastIndexOf, length(), contains, >=, >, &&, ||, !, ==
were all operations in the loop with operands that did not change, so they can be taken out of the loop
all together
{
int xPos = str.lastIndexOf('x');
int yPos = str.lastIndexOf('y');
int len = str.length();
if ((xPos >= 0 && yPos >= 0 && (yPos > xPos)) || (!str.contains("x")) || str.length() == 0) {
return true;
}
}
1:
2:
3:
4:
5:
6:
7:
8:
9:
Select all
Open in new window
can be considered an operation
which one is the loop(for loop right which i commented) which one are operands(the one on which we are acting in the loop namely int i??)
please advise
Since you have an if then else scenario, there is no need for a loop. Why not simply -
return (!str.contains("X") || str.isEmpty()) ? true : str.lastIndexOf("Y") >= str.lastIndexOf("X");
return (!str.contains("X") || str.isEmpty()) ? true : str.lastIndexOf("Y") >= str.lastIndexOf("X");
str.lastIndexOf('x');
lastIndexOf is an operator, with operands str and 'x'
xPos >= 0
>= is an operator, with operands xPos and 0
xPos >= 0 && yPos >= 0
&& is an operator, with operands xPos >= 0 and yPos >= 0
str.contains("x")
contains is an operator, with operands str and "x"
!str.contains("x")
! is an operator, with operand str.contains("x")
str.length()
length is an operator, with operand str
str.length() == 0
== is an operator, with operands str.length() and 0
(Which, by the way, is completely superfluous here. Do you see why?)
xyBalance("aaxbby")
xyBalance is an operator, with operand "aaxbby"
{
int xPos = str.lastIndexOf('x');
int yPos = str.lastIndexOf('y');
int len = str.length();
if ((xPos >= 0 && yPos >= 0 && (yPos > xPos)) || (!str.contains("x")) || str.length() == 0) {
return true;
}
}
is an operator, with operands str, 'x', 'y', 0, "x", true
lastIndexOf is an operator, with operands str and 'x'
xPos >= 0
>= is an operator, with operands xPos and 0
xPos >= 0 && yPos >= 0
&& is an operator, with operands xPos >= 0 and yPos >= 0
str.contains("x")
contains is an operator, with operands str and "x"
!str.contains("x")
! is an operator, with operand str.contains("x")
str.length()
length is an operator, with operand str
str.length() == 0
== is an operator, with operands str.length() and 0
(Which, by the way, is completely superfluous here. Do you see why?)
xyBalance("aaxbby")
xyBalance is an operator, with operand "aaxbby"
{
int xPos = str.lastIndexOf('x');
int yPos = str.lastIndexOf('y');
int len = str.length();
if ((xPos >= 0 && yPos >= 0 && (yPos > xPos)) || (!str.contains("x")) || str.length() == 0) {
return true;
}
}
is an operator, with operands str, 'x', 'y', 0, "x", true
return (!str.contains("X") || str.isEmpty()) ? true : str.lastIndexOf("Y") >= str.lastIndexOf("X");could have been simply
return str.lastIndexOf("y") >= str.lastIndexOf("x");
DOH! I think gudii9 has me thinking things are more complex than they need be. :-)
(!str.contains("X") || str.isEmpty()) == (!str.contains("X"))
ASKER
str.length() == 0no . can you please elaborate?
== is an operator, with operands str.length() and 0
(Which, by the way, is completely superfluous here. Do you see why?)
{i do not see operator above? what is operator name?
int xPos = str.lastIndexOf('x');
int yPos = str.lastIndexOf('y');
int len = str.length();
if ((xPos >= 0 && yPos >= 0 && (yPos > xPos)) || (!str.contains("x")) || str.length() == 0) {
return true;
}
}
is an operator, with operands str, 'x', 'y', 0, "x", true
also i do not see why xPos and yPos is not operands?
x and y are highlighted ones right?
ASKER
return (!str.contains("X") || str.isEmpty()) ? true : str.lastIndexOf("Y") >= str.lastIndexOf("X");
could have been simply
return str.lastIndexOf("y") >= str.lastIndexOf("x");
how?please advise
As mentioned, (!str.contains("X") || str.isEmpty()) == (!str.contains("X")) so isEmpty is unnecessary
But even contains is unnecessary, since lastIndexOf returns -1 if the character does not occur.
http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#lastIndexOf(int)
But even contains is unnecessary, since lastIndexOf returns -1 if the character does not occur.
http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#lastIndexOf(int)
ASKER
public boolean xyBalance(String str) {
//for(int i=0;i<=str.length();i++){
// int xPos= str.lastIndexOf('x');
//int yPos= str.lastIndexOf('y');
// int len=str.length();
// if((xPos>=0&&yPos>=0&&(yPos>xPos))|| (!str.cont ains("x")) || str.length()==0 ){
// return true;
// }
//}
//return false;
return str.lastIndexOf("y") >= str.lastIndexOf("x");
}
above passed all
lastIndexOf
public int lastIndexOf(int ch,
int fromIndex)
Returns the index within this string of the last occurrence of the specified character, searching backward starting at the specified index. For values of ch in the range from 0 to 0xFFFF (inclusive), the index returned is the largest value k such that:
can i write this challenge solution using above method instead searching backwards?
what is operator name?I don't know if it has a name. If you wrote it, I guess you can get to name it.
Or maybe its name is xyBalance, but in that case, I would consider str to be the only operand, with 'x', 'y', 0, "x", true being unchangeable parts of the operator
I might think of xPos as an operand to
xPos >= 0
but not as an operand to
int xPos = str.lastIndexOf('x');
xPos >= 0
since in the latter case it would be internal to the combined operation of the two statements together.
If you prefer to use the word "operation" only when referring to elementary operations like >=
and not to combined operations like (!str.contains("X") || str.isEmpty()),
that would also be an acceptable usage, but then I would want to find a more general term to say
"A loop can be used to apply the same operation on different operands."
can i write this challenge solution using above method instead searching backwards?Above method uses lastIndexOf, which is searching backwards.
But since lastIndexOf is doing it, your code does not have to.
It might also be done searching forwards instead of backwards, but that seems less natural for this problem.
ASKER
Above method uses lastIndexOf, which is searching backwards.
But since lastIndexOf is doing it, your code does not have to.
It might also be done searching forwards instead of backwards, but that seems less natural for this problem.
how to write without failing any test cases?
Are you failing any test cases?
ASKER
return !str.matches(".*x[^y]*");
what is meaing of above code and how to test it in regex101 site with step by step screenshot procedure if possible please??
ASKER
ASKER
https://regex101.com/r/eF4wT3/2
i see you got false.
can you please explain each section that we need to enter in regex101 site to get the output?
i see substitution section has false? is substitution same as output section?
ASKER
https://regex101.com/r/eF4wT3/2
i see you got false.
can you please explain each section that we need to enter in regex101 site to get the output?
i see substitution section has false? is substitution same as output section?
according to challenge we should get true right not false as you got in above link?
how to get true?
ASKER
[^y]* match a single character not present in the list below
which list regex101 mean meant when they mention as above in their site?
ASKER
aaxbby
aaxbb unbalanced
yaaxbb unbalanced
yaaxbby
xaxxbby
xaxxbbyx unbalanced
xxbxy
bbb
bxbb unbalanced
bxyb
x unbalanced
y
yxyxyxyx unbalanced
yxyxyxyxy
other tests
how to print balanced as well similar to unbalanced as below
aaxbby balanced
aaxbb unbalanced
yaaxbb unbalanced
yaaxbby balanced
xaxxbby balanced
xaxxbbyx unbalanced
xxbxy balanced
bbb balanced
bxbb unbalanced
bxyb balanced
x unbalanced
y
yxyxyxyx unbalanced
yxyxyxyxy balanced
other tests
I substituted the string "false" instead of "true" because the java code would have !negated the result of the matches call.
That may have been a poor choice if it caused confusion.
I tried to be more clear by substituting "$0 unbalanced"
We might match the balanced lines instead of the unbalanced lines with a (?!negative lookahead) regular expression
https://regex101.com/r/eF4wT3/5
but that adds complication that gets us further afield of the java code we were trying to elucidate.
To generate either "balanced" or "unbalanced" with a single substitution would require a substitution that conditionally depended on what was matched, which is not possible within the regex101.com substitution syntax.
That may have been a poor choice if it caused confusion.
I tried to be more clear by substituting "$0 unbalanced"
We might match the balanced lines instead of the unbalanced lines with a (?!negative lookahead) regular expression
https://regex101.com/r/eF4wT3/5
but that adds complication that gets us further afield of the java code we were trying to elucidate.
To generate either "balanced" or "unbalanced" with a single substitution would require a substitution that conditionally depended on what was matched, which is not possible within the regex101.com substitution syntax.
ASKER
To generate either "balanced" or "unbalanced" with a single substitution would require a substitution that conditionally depended on what was matched, which is not possible within the regex101.com substitution syntax.ok
I substituted the string "false" instead of "true" because the java code would have !negated the result of the matches call.unbalanced is more clear approach to me
That may have been a poor choice if it caused confusion.
I tried to be more clear by substituting "$0 unbalanced"
We might match the balanced lines instead of the unbalanced lines with a (?!negative lookahead) regular expression
not clear what you meant by ?!negative lookahead
please advise
not clear what you meant by ?!negative lookaheadThat's why I didn't want to use it in the first illustration, and showed a regexp that matched xyunbalanced lines instead of xybalanced liines.
However, Java regular expression constructs are all listed here: https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html
Most of them seem similar enough to the notation used by regex101.com that the latter can be used for illustrative purposes, but the more subtle features you use, the more chances there are for divergence between implementations.
ASKER
That's why I didn't want to use it in the first illustration, and showed a regexp that matched xyunbalanced lines instead of xybalanced liines.
which is first which is second. what you mean by did not want to use?
which one you used in second illustration and where you used it?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.