gudii9
asked on
xyzThere java challenge
Hi,
I am working on below challenge
http://codingbat.com/prob/p136594
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/p136594
I tried my code as below
public boolean xyzThere(String str) {
int xPos= str.indexOf('x');
int len=str.length();
String sub=str.substring(xPos,len);
if(sub.equals("xyz")){
return true;
}
else{
return false;
}
}
I am getting below resultExpected Run
xyzThere("abcxyz") → true true OK
xyzThere("abc.xyz") → false true X
xyzThere("xyz.abc") → true false X
xyzThere("abcxy") → false false OK
xyzThere("xyz") → true true OK
xyzThere("xy") → false false OK
xyzThere("x") → false false OK
xyzThere("") → false Exception:java.lang.StringIndexOutOf BoundsExce ption: String index out of range: -1 (line number:5) X
xyzThere("abc.xyzxyz") → true false X
xyzThere("abc.xxyz") → true false X
xyzThere(".xyz") → false true X
xyzThere("12.xyz") → false true X
xyzThere("12xyz") → true true OK
xyzThere("1.xyz.xyz2.xyz")→ false false OK
other tests
X
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
.* any character except \n (0 or more times
(matching the most amount possible))
[^.] any character except: '.'
(.*[^.])? optional group
xyz 'xyz'
.* any character except \n (0 or more times
(matching the most amount possible))
i.e. 'xyz' optionally preceded by a string ending with something other than '.'
(matching the most amount possible))
[^.] any character except: '.'
(.*[^.])? optional group
xyz 'xyz'
.* any character except \n (0 or more times
(matching the most amount possible))
i.e. 'xyz' optionally preceded by a string ending with something other than '.'
ASKER
public boolean xyzThere(String str) {
//int xPos= str.indexOf('x');
// int len=str.length();
String strNew="xyz";
String strDot=".xyz";
// String sub=str.substring(xPos,len);
//if(sub.equals("xyz")){
if(str.contains(strNew)&&!str.contains(strDot)){
return true;
}
else{
return false;
}
}
i wrote as above and failing one test.
How to fix that test also
Not clear how below shoud be true??
xyzThere("abc.xyzxyz") → true false X
As the challenge says
Return true if the given string contains an appearance of "xyz" where the xyz is not directly preceeded by a period (.). So "xxyz" counts but "x.xyz" does not.
xyzThere("abcxyz") → true
xyzThere("abc.xyz") → false
xyzThere("xyz.abc") → true
xyzThere("abc.xyzxyz") → counts
xyzThere("abc.xyzxyz") → does not
xyzThere("abc.xyzxyz") → does not
ASKER
Both look same to my eyes
ASKER
xyzThere("abc.xyzxyz") → counts
xyzThere("abc.xyzxyz") → does not
above both looks same to me. I am not able to notice any difference. please advise
ASKER
xyzThere("abc.xyzxyz") → true false X
how above is true as per below explanation?
how above is true as per below explanation?
Return true if the given string contains an appearance of "xyz" where the xyz is not directly preceeded by a period (.). So "xxyz" counts but "x.xyz" does not.
xyzThere("abcxyz") → true
xyzThere("abc.xyz") → false
xyzThere("xyz.abc") → true
ASKER
package simple.servlet;
public class XyzThere {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("value is--->"+xyzThere("abc.xyzxyz")); }
public static boolean xyzThere(String str) {
//int xPos= str.indexOf('x');
// int len=str.length();
String strNew="xyz";
String strDot=".xyz";
// String sub=str.substring(xPos,len);
//if(sub.equals("xyz")){
if(str.contains(strNew)&&str.contains(strDot)){
return true;
}
else{
return false;
}
}
}
above passed one test case as below
value is--->true
but failing others
public boolean xyzThere(String str) {
//int xPos= str.indexOf('x');
// int len=str.length();
String strNew="xyz";
String strDot=".xyz";
// String sub=str.substring(xPos,len);
//if(sub.equals("xyz")){
if(str.contains(strNew)&&str.contains(strDot)){
return true;
}
else{
return false;
}
}
Expected Run
xyzThere("abcxyz") → true false X
xyzThere("abc.xyz") → false true X
xyzThere("xyz.abc") → true false X
xyzThere("abcxy") → false false OK
xyzThere("xyz") → true false X
xyzThere("xy") → false false OK
xyzThere("x") → false false OK
xyzThere("") → false false OK
xyzThere("abc.xyzxyz") → true true OK
xyzThere("abc.xxyz") → true false X
xyzThere(".xyz") → false true X
xyzThere("12.xyz") → false true X
xyzThere("12xyz") → true false X
xyzThere("1.xyz.xyz2.xyz") → false true X
other tests
X
Your progress graph for this problem
Return true if the given string contains an appearance of "xyz" where the xyz is not directly preceeded by a period (.). So "xxyz" counts but "x.xyz" does not.
xyzThere("abc.xyzxyz") → true false X
Is an appearance of "xyz" where the xyz is not directly preceeded by a period (.)
(it is directly preceeded by a z)
xyzThere("abc.xyzxyz") → true false X
Is an appearance of "xyz" where the xyz is not directly preceeded by a period (.)
(it is directly preceeded by a z)
ASKER
now i finally got meaning of description of challenge
xyzThere("abc.xyzxyz") → true false X
why only above test failing with below my code approach?
xyzThere("abc.xyzxyz") → true false X
why only above test failing with below my code approach?
public boolean xyzThere(String str) {
//int xPos= str.indexOf('x');
// int len=str.length();
String strNew="xyz";
String strDot=".xyz";
// String sub=str.substring(xPos,len);
//if(sub.equals("xyz")){
if(str.contains(strNew)&&!str.contains(strDot)){
return true;
}
else{
return false;
}
}
xyzThere("abc.xyzxyz") → true false X
String strNew="xyz";
String strDot=".xyz";
"abc.xyzxyz".contains(strNew) is true
"abc.xyzxyz".contains(strDot) is true
true&&!true is false
String strNew="xyz";
String strDot=".xyz";
"abc.xyzxyz".contains(strNew) is true
"abc.xyzxyz".contains(strDot) is true
true&&!true is false
ASKER
xyzThere("abc.xyzxyz") → true false X
String strNew="xyz";
String strDot=".xyz";
"abc.xyzxyz".contains(strNew) is true
"abc.xyzxyz".contains(strDot) is true
true&&!true is false
how to deal this special case where both are present strNew, strDot??
ASKER
Return true if the given string contains an appearance of "xyz" where the xyz is not directly preceeded by a period (.). So "xxyz" counts but "x.xyz" does not.challenge did not tell what to do if both are there together?
please advise
what to do if both are there together
Return true if the given string contains an appearance of "xyz" where the xyz is not directly preceeded by a periodIf both are there together, does the string contain an appearance of "xyz" where the xyz is not directly preceded by a period?
ASKER
If both are there together, does the string contain an appearance of "xyz" where the xyz is not directly preceded by a period?
no i think.
can you please elaborate your question. I think i was not 100% clear on the question
you are asking if xyz and ,xyz together then what result shoudl cime?
xyz there true
.xyz there result should be false
if both xyz and .xyz together not sure what challenge expecging based on the chalenge description.
xyzThere("abc.xyzxyz") → true
both are there together
"abc.xyzxyz"
is an appearance of "xyz" where the xyz is not directly preceded by a period
the string contains this appearance
both are there together
"abc.xyzxyz"
is an appearance of "xyz" where the xyz is not directly preceded by a period
the string contains this appearance
ASKER
I got it.
eventhough strDot there as strNew there we should return true only which is not mentioned explicitly in challenge
above fails more tests
please advise
eventhough strDot there as strNew there we should return true only which is not mentioned explicitly in challenge
public boolean xyzThere(String str) {
//int xPos= str.indexOf('x');
// int len=str.length();
String strNew="xyz";
String strDot=".xyz";
// String sub=str.substring(xPos,len);
//if(sub.equals("xyz")){
if(str.contains(strNew)){
return true;
}
else if(str.contains(strDot)){
return false;
}
else{
return false;
}
}
above fails more tests
Expected Run
xyzThere("abcxyz") → true true OK
xyzThere("abc.xyz") → false true X
xyzThere("xyz.abc") → true true OK
xyzThere("abcxy") → false false OK
xyzThere("xyz") → true true OK
xyzThere("xy") → false false OK
xyzThere("x") → false false OK
xyzThere("") → false false OK
xyzThere("abc.xyzxyz") → true true OK
xyzThere("abc.xxyz") → true true OK
xyzThere(".xyz") → false true X
xyzThere("12.xyz") → false true X
xyzThere("12xyz") → true true OK
xyzThere("1.xyz.xyz2.xyz") → false true X
other tests
X
Correct for more than half the tests
please advise
xyzThere("abc.xyz") → false
"abc.xyz" does not contain an appearance of "xyz" where the xyz is not directly preceded by a period
"abc.xyz" does not contain an appearance of "xyz" where the xyz is not directly preceded by a period
ASKER
i tried sdrawing flow chart at
http://drawisland.com/
not able to save or attach here to narrow down the mistake i am doing
http://drawisland.com/
not able to save or attach here to narrow down the mistake i am doing
ASKER
public boolean xyzThere(String str) {
String strNew="xyz";
String strDot=".xyz";
if(str.contains(strDot)){
return false;
}
else if(str.contains(strNew)){
return true;
}
else if(str.contains(strNew)&&str.contains(strDot)){
return false;
}
else
return false;
}
above also failing one
Expected Run
xyzThere("abcxyz") → true true OK
xyzThere("abc.xyz") → false false OK
xyzThere("xyz.abc") → true true OK
xyzThere("abcxy") → false false OK
xyzThere("xyz") → true true OK
xyzThere("xy") → false false OK
xyzThere("x") → false false OK
xyzThere("") → false false OK
xyzThere("abc.xyzxyz") → true false X
xyzThere("abc.xxyz") → true true OK
xyzThere(".xyz") → false false OK
xyzThere("12.xyz") → false false OK
xyzThere("12xyz") → true true OK
xyzThere("1.xyz.xyz2.xyz")
other tests
X
not able to fix that one test case
xyzThere("abc.xyzxyz") → true false X
please advise
ASKER
public boolean xyzThere(String str) {
String strNew="xyz";
String strDot=".xyz";
if(str.contains(strNew)&&str.contains(strDot)){
return true;
}
else if(str.contains(strDot)){
return false;
}
else if(str.contains(strNew)){
return true;
}
else
return false;
}
this passed that test but failing other testsExpected Run
xyzThere("abcxyz") → true true OK
xyzThere("abc.xyz") → false true X
xyzThere("xyz.abc") → true true OK
xyzThere("abcxy") → false false OK
xyzThere("xyz") → true true OK
xyzThere("xy") → false false OK
xyzThere("x") → false false OK
xyzThere("") → false false OK
xyzThere("abc.xyzxyz") → true true OK
xyzThere("abc.xxyz") → true true OK
xyzThere(".xyz") → false true X
xyzThere("12.xyz") → false true X
xyzThere("12xyz") → true true OK
xyzThere("1.xyz.xyz2.xyz")→ false true X
other tests
X
how to pass all test cases?
please advise
ASKER
xyzThere(".xyz") → false true X
xyzThere("12.xyz") → false true X
why above expected as true?
according to challenge i though above should expected should be true
Return true if the given string contains an appearance of "xyz" where the xyz is not directly preceeded by a period (.). So "xxyz" counts but "x.xyz" does not.
xyzThere("abcxyz") → true
xyzThere("abc.xyz") → false
xyzThere("xyz.abc") → true
ASKER
package simple.servlet;
public class XyzThere {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("value is--->"+xyzThere("abc.xyz")); }
public static boolean xyzThere(String str) {
String strNew="xyz";
String strDot=".xyz";
if(str.contains(strNew)&&str.contains(strDot)){
return true;
}
else if(str.contains(strDot)){
return false;
}
else if(str.contains(strNew)){
return true;
}
else
return false;
}
}
when i debug it is wrongly going to first if loop which i do not want i want to send it to if else not if loop??
ASKER
how to differentiate .xyz and xyz say something like strictly match xyz or strictly matches .xyz
as attached
errXyz.png
as attached
errXyz.png
What are the possible combinations of true/false values for str.contains(strNew) and str.contains(strDot)?
Which of those possible combinations determine a value for xyzThere(str)?
Which of those possible combinations determine a value for xyzThere(str)?
public boolean xyzThere(String str) {
return str.matches("(.*[^.])?xyz.*");
}
public boolean xyzThere(String str) {
return str.replaceAll("\\.xyz",".").contains("xyz");
}
public boolean xyzThere(String str) {
return str.startsWith("xyz") || str.length()>0 && xyzThere(str.replaceFirst("\\.*.",""));
}
public boolean xyzThere(String str) {
for( int i=0;i<str.length(); ++i ){
if( (i==0||!str.startsWith(".",i-1)) && str.startsWith("xyz",i) ){
return true;
}
}
return false;
}
ASKER
how to make my approach work ??
is it is possible?
public boolean xyzThere(String str) {
String strNew="xyz";
String strDot=".xyz";
if(str.contains(strNew)&&str.contains(strDot)){
return true;
}
else if(str.contains(strDot)){
return false;
}
else if(str.contains(strNew)){
return true;
}
else
return false;
}
is it is possible?
ASKER
return str.matches("
(.*[^.])
?xyz.*"
);
what is the meaning and logic behind above code?
ASKER
public boolean xyzThere(String str) {
for( int i=0;i<str.length(); ++i ){
if( (i==0||!str.startsWith(".",i-1)) && str.startsWith("xyz",i) ){
return true;
}
}
return false;
}
what is logic behind aboe
ASKER
public boolean xyzThere(String str) {
return str.startsWith("xyz") || str.length()>0 && xyzThere(str.replaceFirst("\\.*.",""));
}
in above approach what is meaning of
xyzThere(str.replaceFirst(
this is recursion approach right?
calling same function and replacing . with "" to make .xyz as xyz?
ASKER
can we solve this without using regular expression without recursion using simle if else if loops?
how to make my approach work ?Understand the potentials and limits of the approach.
What can knowing the values of str.contains(strNew) and str.contains(strDot) tell you about str?
What can't knowing the values of str.contains(strNew) and str.contains(strDot) tell you about str?
What do you want to know about str?
How do the possible combinations of str.contains(strNew) values and str.contains(strDot) relate to that?
Is there anything else you need to know about str?
public boolean xyzThere(String str) {
for( String x : str.split("\\.xyz") ){
if( x.contains("xyz") ){ return true; }
}
return false;
}
public boolean xyzThere(String str) {
for( int i=0;i<=str.length(); ++i ){
if( str.substring(0,i).endsWith( "xyz")
&& !str.substring(0,i).endsWith(".xyz")
){
return true;
}
}
return false;
}
public boolean xyzThere(String str) {
for( int i=0;i<=str.length()-4; i+=1 ){
if( str.substring(i+1,i+4).contains( "xyz")
&& !str.substring(i ,i+4).contains(".xyz")
){
return true;
}
}
return str.startsWith("xyz");
}
public boolean xyzThere(String str) {
return str.replaceAll("\\.xyz",".").length()!=str.replaceAll("xyz","").length();
}
ASKER
public boolean xyzThere(String str) {
String strNew="xyz";
String strDot=".xyz";
if(str.contains(strNew)&&!str.contains(strDot)){
return true;
}
else
return false;
}
how below worked whereas above did not work??what is the need of for loop? please advise
public boolean xyzThere(String str) {
for( int i=0;i<=str.length(); ++i ){
if( str.substring(0,i).endsWith( "xyz")
&& !str.substring(0,i).endsWith(".xyz")
){
return true;
}
}
return false;
}
ASKER
changing ++i to i++ has any impact?
i do not see any impact in test results?
also substring(0,1) and substring(0,2) are not necessary to compare with xyz..i guess my question is need of for loop here??
i do not see any impact in test results?
also substring(0,1) and substring(0,2) are not necessary to compare with xyz..i guess my question is need of for loop here??
ASKER
public class XyzThereEx {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("value is-->"+xyzThere("abc.xyzxyz"));
}
public static boolean xyzThere(String str) {
for( String x : str.split("\\.xyz") ){
System.out.println("x value is-->"+x.toString());
if( x.contains("xyz") ){ return true; }
}
return false;
}
}
how split works?
x value is-->abc
x value is-->xyz
value is-->true
ASKER
http://www.tutorialspoint.com/java/java_string_split.htm
ilooked example
i did not understood below program and output
ilooked example
i did not understood below program and output
import java.io.*;
public class Test{
public static void main(String args[]){
String Str = new String("Welcome-to-Tutorialspoint.com");
System.out.println("Return Value :" );
for (String retval: Str.split("-", 2)){
System.out.println(retval);
}
System.out.println("");
System.out.println("Return Value :" );
for (String retval: Str.split("-", 3)){
System.out.println(retval);
}
System.out.println("");
System.out.println("Return Value :" );
for (String retval: Str.split("-", 0)){
System.out.println(retval);
}
System.out.println("");
System.out.println("Return Value :" );
for (String retval: Str.split("-")){
System.out.println(retval);
}
}
}
This produces the following result:
Return Value :
Welcome
to-Tutorialspoint.com
Return Value :
Welcome
to
Tutorialspoint.com
Return Value:
Welcome
to
Tutorialspoint.com
Return Value :
Welcome
to
Tutorialspoint.com
++i, i++, and i+=1 all have the same side effect
unless you try to use the resulting value
unless you try to use the resulting value
substring(0,1) and substring(0,2) are not necessaryGood observation.
So you could have changed the loop bounds without changing the result.
(substring(0,0) is also not necessary)
what is the need of for loop?How much can knowing the results of str.contains(strNew) and str.contains(strDot) tell you about xyzThere("abc.xyzxyz") and xyzThere("abc.xyz")
ASKER
until bolded z till then both are same after that only second string is missing rest of group of xyz characters?
abc.xyzxyz
abc.xyzxyz
And what are the values of str.contains(strNew) and str.contains(strDot)?
ASKER
both true
So when both str.contains(strNew) and str.contains(strDot) are true, what does that tell you about xyzThere(str)?
ASKER
So when both str.contains(strNew) and str.contains(strDot) are true, what does that tell you about xyzThere(str)?
it tells it should be true as per challenge description
Return true if the given string contains an appearance of "xyz" where the xyz is not directly preceeded by a period (.). So "xxyz" counts but "x.xyz" does not.
xyzThere("abcxyz") → true
xyzThere("abc.xyz") → false
xyzThere("xyz.abc") → true
Go.
xyzThere("abc.xyzxyz") → true
public boolean xyzThere(String str) {
for( int i=0;i<=str.length(); i++ ){
if( str.substring(0,i).endsWith( "xyz")
&& !str.substring(0,i).endsWith(".xyz")
){
return true;
}
}
return false;
}
ASKER
public boolean xyzThere(String str) {
for( int i=0;i<=str.length(); i++ ){
if( str.substring(0,i).endsWith( "xyz")
&& !str.substring(0,i).endsWith(".xyz")
){
return true;
}
}
return false;
}
Also why we choose only endsWith() functiuon rather than beginsWith() etc function??
xyzThere("abc.xyz") → false
Does "abc.xyz" contain "xyz"?
Does "abc.xyz" contain "xyz"?
why we choose endsWith() rather than beginsWith()I thought it made the logic seem simpler, since "xyz" and ".xyz" can end at the same place although they don't begin at the same place.
here's the same logic using startsWith
for( int i=0;i<=str.length(); i++ ){
if( str.substring(i).startsWith( "xyz")
&& !(i>0&&str.substring(i-1).startsWith(".xyz"))
){
return true;
}
}
But that could have been written
public boolean xyzThere(String str) {
for( int i=0;i<=str.length(); i++ ){
if( str.startsWith( "xyz",i)
&& (i==0||!str.startsWith(".",i-1))
){
return true;
}
}
return false;
}
so that could be simpler from another perspective
ASKER
still not clear why we need i and then for loop both startsWith and endsWith approach
please advise
please advise
Because "abc.xyz" and "abc.xyzxyz" both contain "xyz"
so str.contains("xyz") and str.contains(".xyz") are not sufficient to distinguish between
xyzThere("abc.xyz") and xyzThere("abc.xyzxyz")
so str.contains("xyz") and str.contains(".xyz") are not sufficient to distinguish between
xyzThere("abc.xyz") and xyzThere("abc.xyzxyz")
ASKER
so str.contains("xyz") and str.contains(".xyz") are not sufficient to distinguish between
xyzThere("abc.xyz") and xyzThere("abc.xyzxyz")
yes. i see this is kind of not clear earlier now make sense.
/
why we need i and for loop?
ASKER
public boolean xyzThere(String str) {
//public boolean xyzThere(String str) {
for( int i=0;i<=str.length(); i++ ){
if( str.startsWith( "xyz",i)
&& (i==0||!str.startsWith(".",i-1))
){
return true;
}
}
return false;
}
public boolean xyzThere(String str) {
for( int i=0;i<=str.length(); i++ ){
if( str.substring(0,i).endsWith( "xyz")
&& !str.substring(0,i).endsWith(".xyz")
){
return true;
}
}
return false;
}
why we used i-1??
ASKER
why we need to increment i?
Because that's where the "." would be when ".xyz".startsWith("xyz",i) is true
ASKER
if .xyz comes in middle how we can handle that scenario not at start not at end?
By looping for( int i=0;i<=str.length(); ++i )
ASKER
public class XyzThereEx {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("value is-->"+xyzThere("abc.xyzxyz"));
}
public static boolean xyzThere(String str) {
for( String x : str.split("\\.xyz") ){
System.out.println("x value is-->"+x.toString());
if( x.contains("xyz") ){ return true; }
}
return false;
}
}
when i ran above i got below correct output
x value is-->abc
x value is-->xyz
value is-->true
what is meaning of //.
please advise
ASKER
http://www.tutorialspoint.com/java/java_string_startswith.htm'
Here is the syntax of this method:
public boolean startsWith(String prefix, int toffset)
or
public boolean startsWith(String prefix)
Parameters:
Here is the detail of parameters:
prefix -- the prefix to be matched.
toffset -- where to begin looking in the string.
Return Value:
It returns true if the character sequence represented by the argument is a prefix of the character sequence represented by this string; false otherwise.
Example:
i see as above
my question is in below solutuon why we used i-1 instead of i in below line?
if( str.startsWith( "xyz",i) && (i==0||!str.startsWith("." ,i-1))
Here is the syntax of this method:
public boolean startsWith(String prefix, int toffset)
or
public boolean startsWith(String prefix)
Parameters:
Here is the detail of parameters:
prefix -- the prefix to be matched.
toffset -- where to begin looking in the string.
Return Value:
It returns true if the character sequence represented by the argument is a prefix of the character sequence represented by this string; false otherwise.
Example:
i see as above
my question is in below solutuon why we used i-1 instead of i in below line?
if( str.startsWith( "xyz",i) && (i==0||!str.startsWith("."
ASKER
also why we need i==0??
if( str.startsWith( "xyz",i) && (i==0||!str.startsWith(".",i-1) )
if( str.startsWith( "xyz",i) && (i==0||!str.startsWith(".",i-1)
ASKER
public class XyzThereEx {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("value is-->"+xyzThere("abcxyz.xyz"));
}
public static boolean xyzThere(String str) {
//public boolean xyzThere(String str) {
for( int i=0;i<=str.length(); i++ ){
if( str.startsWith( "xyz",i) && (!str.startsWith(".",i-1))
){
return true;
}
}
return false;
}
}
above gave correct output true without i==0??
ASKER
public boolean xyzThere(String str) {
//public boolean xyzThere(String str) {
for( int i=0;i<=str.length(); i++ ){
if( str.startsWith( "xyz",i)&& (!str.startsWith(".",i-1))
){
return true;
}
}
return false;
}
i see you are checking if given string starts with xyz say from index 3 and also same time checking if given string not startts with . from say index 2 so we are taking care of both xyz and .xyz case even if they come together we are covered.
public boolean xyzThere(String str) {
//public boolean xyzThere(String str) {
for( int i=0;i<=str.length(); i++ ){
if( str.startsWith( "xyz",i)&& (!str.startsWith(".",i-1))
){
return true;
}
}
return false;
}
as above we got all tests passed even without i==0Expected Run
xyzThere("abcxyz") → true true OK
xyzThere("abc.xyz") → false false OK
xyzThere("xyz.abc") → true true OK
xyzThere("abcxy") → false false OK
xyzThere("xyz") → true true OK
xyzThere("xy") → false false OK
xyzThere("x") → false false OK
xyzThere("") → false false OK
xyzThere("abc.xyzxyz") → true true OK
xyzThere("abc.xxyz") → true true OK
xyzThere(".xyz") → false false OK
xyzThere("12.xyz") → false false OK
xyzThere("12xyz") → true true OK
xyzThere("1.xyz.xyz2.xyz")
other tests
OK
ASKER
public boolean xyzThere(String str) {
//public boolean xyzThere(String str) {
for( int i=0;i<=str.length(); ++i ){
if( str.endsWith( "xyz")&& (!str.endsWith(".xyz"))
){
return true;
}
}
return false;
}
above failing one, i wonder whyExpected Runwhy we need substring for endsWith approach.
xyzThere("abcxyz") → true true OK
xyzThere("abc.xyz") → false false OK
xyzThere("xyz.abc") → true false X
xyzThere("abcxy") → false false OK
xyzThere("xyz") → true true OK
xyzThere("xy") → false false OK
xyzThere("x") → false false OK
xyzThere("") → false false OK
xyzThere("abc.xyzxyz") → true true OK
xyzThere("abc.xxyz") → true true OK
xyzThere(".xyz") → false false OK
xyzThere("12.xyz") → false false OK
xyzThere("12xyz") → true true OK
xyzThere("1.xyz.xyz2.xyz")→ false false OK
other tests
X
Does ends with do not have method like??
public boolean endsWith(String prefix, int toffset)
ASKER
public class XyzThereEx {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("value is 11-->"+xyzThere("xyz.abc"));
System.out.println("value is 22-->"+xyzThere("abcxyz"));
System.out.println("value is 33-->"+xyzThere("abc.xyzxyz"));
}
public static boolean xyzThere(String str) {
//public boolean xyzThere(String str) {
for( int i=0;i<=str.length(); ++i ){
if( str.endsWith( "xyz")&& (!str.endsWith(".xyz"))
){
return true;
}
}
return false;
}
}
above gave below output
value is 11-->false
value is 22-->true
value is 33-->true
i was not sure why i am getting false for below
value is 11-->false
i thought i should get true?
ASKER
if( str.substring(0,i).endsWith( "xyz")&& !str.substring(0,i).endsWi th(".xyz")
i thought above should be as below
if( str.substring(0,i).endsWit
since .xyz is of one character(total 4 characters) more than xyz(total 3 characters??
please advise
above gave correct output true without i==0??You are correct.
I had been thinking of
!str.substring(i-1).starts
which would need to guard against i==0,
but unlike substring(-1), which causes an error, startsWith(".xyz",-1) returns false, so doing it this way there is no need to check for a special case.
str.substring(0,i).endsWitwith xyzThere("1.xyz.xyz2.xyz")h( "xyz")&& !str.substring(0,i+1).ends With(".xyz ")
When i==5
str.substring(0,5) is "1.xyz", which endsWith( "xyz")
str.substring(0,5+1) is "1.xyz." which does not end with ".xyz" but that's not what we need to check
System.out.println("value is 11-->"+xyzThere("xyz.abc")str is always "xyz.abc", regardless of what i might be);
str.endsWith( "xyz")&& (!str.endsWith(".xyz")
value is 11-->false
"xyz.abc".endsWith( "xyz") is always false, regardless of what i might be
ASKER
public class XyzThereEx4 {
/**
* @param args
*
*
Return true if the given string contains an appearance of "xyz" where the xyz is not directly preceeded by a period (.). So "xxyz" counts but "x.xyz" does not.
xyzThere("abcxyz") → true
xyzThere("abc.xyz") → false
xyzThere("xyz.abc") → true
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("value is 11-->"+xyzThere("xyz.abc") );
System.out.println("value is 22-->"+xyzThere("abcxyz")) ;
System.out.println("value is 33-->"+xyzThere("abc.xyzxy z"));
System.out.println("value is 44-->"+xyzThere("1.xyz.xyz 2.xyz"));
}
public static boolean xyzThere(String str) {
for( int i=0;i<=str.length(); i++ ){
if( str.substring(0,i).endsWit h( "xyz")&& !str.substring(0,i).endsWi th(".xyz")
){
return true;
}
}
return false;
}
}
/**
* @param args
*
*
Return true if the given string contains an appearance of "xyz" where the xyz is not directly preceeded by a period (.). So "xxyz" counts but "x.xyz" does not.
xyzThere("abcxyz") → true
xyzThere("abc.xyz") → false
xyzThere("xyz.abc") → true
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("value is 11-->"+xyzThere("xyz.abc")
System.out.println("value is 22-->"+xyzThere("abcxyz"))
System.out.println("value is 33-->"+xyzThere("abc.xyzxy
System.out.println("value is 44-->"+xyzThere("1.xyz.xyz
}
public static boolean xyzThere(String str) {
for( int i=0;i<=str.length(); i++ ){
if( str.substring(0,i).endsWit
){
return true;
}
}
return false;
}
}
ASKER
str is always "xyz.abc", regardless of what i might be
"xyz.abc".endsWith( "xyz") is always false, regardless of what i might be
str.substring(0,i).endsWith( "xyz")&& !str.substring(0,i+1).ends With(".xyz ")
with xyzThere("1.xyz.xyz2.xyz")
When i==5
str.substring(0,5) is "1.xyz", which endsWith( "xyz")
str.substring(0,5+1) is "1.xyz." which does not end with ".xyz" but that's not what we need to check
if( str.substring(0,i).endsWith( "xyz")&& !str.substring(0,i).endsWi th(".xyz")
i see the point.
we are incrementally going using string substring with i and checking to see if ends with xyz and same time not end with .xyz
ASKER
.* any character except \n (0 or more times
(matching the most amount possible))
[^.] any character except: '.'
(.*[^.])? optional group
xyz 'xyz'
.* any character except \n (0 or more times
(matching the most amount possible))
i.e. 'xyz' optionally preceded by a string ending with something other than '.'
public boolean xyzThere(String str) {
return str.matches("(.*[^.])?xyz.*");
}
in aboeve solution
what is the meaning and use of optional group
(.*[^.])? optional group
.* any character except \n (0 or more times
(matching the most amount possible))
why except \?
return str.matches("(.*[^.])?xyz.*");
in above what is meaning of ?
is it like if true do something
if false do something else
in a Java regular expression, ? makes the thing before the ? optional
so
"!xyz" matches, because "!" matches [^.]
"xyz" matches, because "" matches (.*[^.])?
so
"!xyz" matches, because "!" matches [^.]
"xyz" matches, because "" matches (.*[^.])?
why except \n(I presume you meant \n, not \?)
Java regular expressions evolved from a style of regular expression that was often used on single lines.
It became common to use .* to mean "everything up to the end of the line"
When this style of regular expression was adapted to languages that also use strings containing multiple lines, it was convenient to maintain the accustomed meaning, and introduce other ways to deal with matches intended to span multiple lines.
In this challenge, all the strings in all the examples were single lines, so dealing with \n was not necessary, and it was simpler not to worry about it. But although all the strings were in fact single lines, the problem specification does not promise that str must always be a single line, so to be completely correct, it would also need to deal with multiple lines containing \n
ASKER
public boolean xyzThere(String str) {i am trying to understand all above comments.
return str.matches("(.*[^.])?xyz.*");
}
in aboeve solution
what is the meaning and use of optional group
(.*[^.])? optional group
.* any character except \n (0 or more times
(matching the most amount possible))
why except \?
return str.matches("(.*[^.])?xyz.*");
in above what is meaning of ?
is it like if true do something
in a Java regular expression, ? makes the thing before the ? optional
so
"!xyz" matches, because "!" matches [^.]
"xyz" matches, because "" matches (.*[^.])?
why except \n
(I presume you meant \n, not \?)
Java regular expressions evolved from a style of regular expression that was often used on single lines.
It became common to use .* to mean "everything up to the end of the line"When this style of regular expression was adapted to languages that also use strings containing multiple lines, it was convenient to maintain the accustomed meaning, and introduce other ways to deal with matches intended to span multiple lines.In this challenge, all the strings in all the examples were single lines, so dealing with \n was not necessary, and it was simpler not to worry about it. But although all the strings were in fact single lines, the problem specification does not promise that str must always be a single line, so to be completely correct, it would also need to deal with multiple lines containing \n
return str.matches("(.*[^.])?xyz.*");
how do you read above?
is it like
if given string line matches all characters except dot then xyz or else all the character?
i think i am lost on flow of above line to read and understand?
can you please summarize?
how do you read above?str can optionally start with a string of characters ending in a character other than '.'
followed by "xyz"
followed by anything
e.g. "abc.xyzxyz"
(.*[^.])?xyz.* matches here "abc.xyzxyz"
(.*[^.])?xyz.* matches here "abc.xyzxyz"
(.*[^.])?xyz.* matches here "abc.xyzxyz"
(.*[^.])?xyz.* matches here "abc.xyzxyz"
(.*[^.])?xyz.* matches "abc.xyzxyz<here>"
(.*[^.])?xyz.* matches "abc.xyzxyz"
"xyz.abc"
(.*[^.])?xyz.* matches "<here>xyz.abc"
(.*[^.])?xyz.* matches here "xyz.abc"
(.*[^.])?xyz.* matches here "xyz.abc"
(.*[^.])?xyz.* matches "xyz.abc"
ASKER
do we need to read left to write like english or right to left like urdu/arabic?
"!xyz" matches, because "!" matches [^.]
"xyz" matches, because "" matches (.*[^.])?
i think you are referring strting with xyz which is on the right hand side of below right?
return str.matches("(.*[^.])?xyz. *");
"!xyz" matches, because "!" matches [^.]
"xyz" matches, because "" matches (.*[^.])?
i think you are referring strting with xyz which is on the right hand side of below right?
return str.matches("(.*[^.])?xyz.
ASKER
e.g. "abc.xyzxyz"
(.*[^.])?xyz.* matches here "abc.xyzxyz"//why not z here
(.*[^.])?xyz.* matches here "abc.xyzxyz"//why only z here
(.*[^.])?xyz.* matches here "abc.xyzxyz"//why abc.xyz here
(.*[^.])?xyz.* matches here "abc.xyzxyz"
(.*[^.])?xyz.* matches "abc.xyzxyz<here>"
(.*[^.])?xyz.* matches "abc.xyzxyz"
"xyz.abc"
(.*[^.])?xyz.* matches "<here>xyz.abc"
(.*[^.])?xyz.* matches here "xyz.abc"
(.*[^.])?xyz.* matches here "xyz.abc"
(.*[^.])?xyz.* matches "xyz.abc"
(.*[^.])?xyz.* matches here "abc.xyzxyz" // why not z here
Because when
(.*[^.])?xyz.* matches "abc.xyzxyz"
then
(.*[^.])?xyz.* would match "abc.xyzxyz"
but then
(.*[^.])?xyz.* could not match "abc.xyzxyz"
(.*[^.])?xyz.* matches here "abc.xyzxyz" //why abc.xyz here
because
(.*[^.])?xyz "abc.xyzxyz" is followed by
(.*[^.])?xyz "abc.xyzxyz"
Because when
(.*[^.])?xyz.* matches "abc.xyzxyz"
then
(.*[^.])?xyz.* would match "abc.xyzxyz"
but then
(.*[^.])?xyz.* could not match "abc.xyzxyz"
(.*[^.])?xyz.* matches here "abc.xyzxyz" //why abc.xyz here
because
(.*[^.])?xyz "abc.xyzxyz" is followed by
(.*[^.])?xyz "abc.xyzxyz"
e.g. "!xyz"
(.*[^.])?xyz.* matches here "!xyz"
(.*[^.])?xyz.* matches here "!xyz"
(.*[^.])?xyz.* matches "!xyz<here>"
e.g. "xyz"
(.*[^.])?xyz.* matches "<here>xyz"
(.*[^.])?xyz.* matches here "xyz"
(.*[^.])?xyz.* matches "xyz<here>"
(.*[^.])?xyz.* matches here "!xyz"
(.*[^.])?xyz.* matches here "!xyz"
(.*[^.])?xyz.* matches "!xyz<here>"
e.g. "xyz"
(.*[^.])?xyz.* matches "<here>xyz"
(.*[^.])?xyz.* matches here "xyz"
(.*[^.])?xyz.* matches "xyz<here>"
ASKER
this is going over my head not sure if there is simple step by step tutorial to read before understanding this?
ASKER
(.*[^.])?xyz.* matches here "!xyz"
why not as below?
"!xyz<here>"
ASKER
e.g. "!xyz"
(.*[^.])?xyz.* matches here "!xyz"
(.*[^.])?xyz.* matches here "!xyz"
(.*[^.])?xyz.* matches "!xyz<here>"
e.g. "xyz"
(.*[^.])?xyz.* matches "<here>xyz"
(.*[^.])?xyz.* matches here "xyz"
(.*[^.])?xyz.* matches "xyz<here>"
i think i am kind of getting now .
so for given string ("!xyz)we are checking where the match using the expression (.*[^.])?xyz.*
i think now next step is not 100% clear why matching at beginning (<here>xyz)or at end(xyz<here>) or at middle
The bolding and underlineing are not getting copied in the
Matches happen wherever it is possible for it to happen, but if that causes a different part of the regular expression to fail, then the whole thing fails.
quotesso it is not clear what parts of the expression or string are being asked about.
Matches happen wherever it is possible for it to happen, but if that causes a different part of the regular expression to fail, then the whole thing fails.
Presuming you meant
(.*[^.])?xyz.* matches here "!xyz"
why not as below?
"!xyz<here>"
Because <here> is being used as a visible representation of the empty string at the end,
and an empty string is not xyz
(.*[^.])?xyz.* matches here "!xyz"
why not as below?
"!xyz<here>"
Because <here> is being used as a visible representation of the empty string at the end,
and an empty string is not xyz
ASKER
The bolding and underlineing are not getting copied in the
quotes
is it is a bug?
how to copy underlines in experts eachange to be more specifc?
There are so many responses here that I haven't read them all but, if you want to do this without regular expressions you might try as below. Please forgive me if I've duplicated someone's comment.
return str.replaceAll("\\.xyz", "").indexOf("xyz") > 0;
return str.replaceAll("\\.xyz", "").indexOf("xyz") > 0;
ASKER
str.replaceAll("\\.xyz", "").indexOf("xyz") > 0;we are replacing .xyz with empty right
then finding indexOf xyz which should be more than 0?
can you please elaborate?
ASKER
public boolean xyzThere(String str) {
return str.replaceAll("\\.xyz", "").indexOf("xyz") > 0;
}
package simple.servlet;
public class XyzThere {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("value is-11-->"+xyzThere("abc.xyzxyz")); //true
System.out.println("value is--12->"+xyzThere("abc.xyz"));//false
System.out.println("value is--13->"+xyzThere("abcxyz"));//true
}
public static boolean xyzThere(String str) {
/*String strNew="xyz";
String strDot=".xyz";
if(str.contains(strNew)&&str.contains(strDot)){
return true;
}
else if(str.contains(strDot)){
return false;
}
else if(str.contains(strNew)){
return true;
}
else
return false;
}
System.out.println("value is-11-->"+xyzThere("abc.xyzxyz")); //true
System.out.println("value is--12->"+xyzThere("abc.xyz"));//false
System.out.println("value is--13->"+xyzThere("abcxyz"));//true
*/
System.out.println(" values is--->"+ str.replaceAll("\\.xyz", ""));
return str.replaceAll("\\.xyz", "").indexOf("xyz") > 0;
}
}
Expected Run
xyzThere("abcxyz") → true true OK
xyzThere("abc.xyz") → false false OK
xyzThere("xyz.abc") → true false X
xyzThere("abcxy") → false false OK
xyzThere("xyz") → true false X
xyzThere("xy") → false false OK
xyzThere("x") → false false OK
xyzThere("") → false false OK
xyzThere("abc.xyzxyz") → true true OK
xyzThere("abc.xxyz") → true true OK
xyzThere(".xyz") → false false OK
xyzThere("12.xyz") → false false OK
xyzThere("12xyz") → true true OK
xyzThere("1.xyz.xyz2.xyz")→ false false OK
other tests
OK
above playing two test cases. please advise
ASKER
return str.matches("(.*[^.])?xyz.*");
return str.replaceAll("\\.xyz", "").indexOf("xyz") > 0;
above two sentences are still not 100% clear to me. can you please advise the logic behind them
I haven't read them allhttps://www.experts-exchange.com/viewCodeSnippet.jsp?refID=41452857&rtid=20&icsi=2
but, if you want to do this without regular expressionsTechnically, replaceAll("\\.xyz","") does have a regular expression.
str.replaceAll("\\.xyz", "").indexOf("xyz") > 0
would also give the wrong answer when str="xyz" or str="ax.xyzyz"
ASKER
Technically, replaceAll("\\.xyz","") does have a regular expression.
str.replaceAll("\\.xyz", "").indexOf("xyz") > 0
what is meaning of above line
\\. means what
when we use
\\ to me like commenting in java?
then what itmeans by indexOf >0??
Because the first argument for the replaceAll function is a regular expression and in regular expressions a "." has a special meaning and needs to be escaped using the '\' and in Java the '\' also has a special meaning and needs to be escaped as well.
IndexOf > 0 was meant to be >= 0 meaning the search string exists somewhere in the string. Also, commenting in Java is done with '//' not '\\'.
IndexOf > 0 was meant to be >= 0 meaning the search string exists somewhere in the string. Also, commenting in Java is done with '//' not '\\'.
ASKER
public boolean xyzThere(String str) {
return str.replaceAll("\\.xyz", "").indexOf("xyz") >= 0;
}
above passed all.
i have confusion why i cannot write like belo
public boolean xyzThere(String str) {
return str.replaceAll("\\.xyz", "").indexOf("xyz") [b][u]=[/u][/b]> 0;
}
ASKER
return str.replaceAll("\\.xyz", "").indexOf("xyz") >= 0;
above is clear now.
Below is not clear yet.
return str.matches("(.*[^.])?xyz.
Please advise
regular expressions a "." has a special meaning and needs to be escaped using the '\' and in Java the '\' also has a special meaning and needs to be escaped as well.
what is special meaning of . in regex and \ in java?
i was trying to test here but could not . please advise
https://regex101.com/
what is meaning of above lineDid you read the Java documentation?
Did some part of it seem unclear?
In a Java regular expression, . matches any character.
\. escapes the . to make it match a literal .
In a Java "quoted" string \ escapes the next character, so \\ makes a literal \
indexOf("xyz") is the position of the first occurrence of "xyz" in the string, or -1 if there is no such occurrence.
so indexOf("xyz") > 0 means that the first occurrence is after the first position in the string.
(which was an error since we also wanted to include the case where the first occurrence is at position 0)
return str.replaceAll("\\.xyz", "").indexOf("xyz") >= 0;
would be incorrect when str="x.xyzyz"
because "x.xyzyz".replaceAll("\\.xyz", "") would be "xyz"
and "xyz".indexOf("xyz") would be 0
0>=0 would be true, but
"x.xyzyz" does not contain an appearance of "xyz" where the xyz is not directly preceeded by a period (.)
would be incorrect when str="x.xyzyz"
because "x.xyzyz".replaceAll("\\.xyz", "") would be "xyz"
and "xyz".indexOf("xyz") would be 0
0>=0 would be true, but
"x.xyzyz" does not contain an appearance of "xyz" where the xyz is not directly preceeded by a period (.)
ASKER
In a Java "quoted" string \
what is meaning of quoted string \
can you please elaborate?
ASKER
return str.replaceAll("\\.xyz", "").indexOf("xyz") >= 0;
i kind of got 90% meaning of above
ASKER
return str.matches("(.*[^.])?xyz.*");
above is clear only 50%
ASKER
return str.replaceAll("\\.xyz", "").indexOf("xyz") >= 0;
would be incorrect when str="x.xyzyz"
because "x.xyzyz".replaceAll("\\.xyz", "") would be "xyz"
and "xyz".indexOf("xyz") would be 0
0>=0 would be true, but
above solution passed all tests as below
Expected Run
xyzThere("abcxyz") → true true OK
xyzThere("abc.xyz") → false false OK
xyzThere("xyz.abc") → true true OK
xyzThere("abcxy") → false false OK
xyzThere("xyz") → true true OK
xyzThere("xy") → false false OK
xyzThere("x") → false false OK
xyzThere("") → false false OK
xyzThere("abc.xyzxyz") → true true OK
xyzThere("abc.xxyz") → true true OK
xyzThere(".xyz") → false false OK
xyzThere("12.xyz") → false false OK
xyzThere("12xyz") → true true OK
xyzThere("1.xyz.xyz2.xyz") → false false OK
other tests
OK
x.xyzyz
is not there as one of test cases
x.xyzyz supposed to return true only right?( as 0>=0)
str.replaceAll("\\.xyz", "").==> this simply removes all chase where the period is followed by "xyz" However, if the .xyz was originally preceded by an "x" and followed by "yz" it would create a "xyz" sequence without the dot and return true when the original string had no "xyz" without a dot in front and should have returned false. The same would hold true if there were "xy" before ".xyx" with a "z" afterwards. ozo is absolutely right since the initial string contained no appearance of "xyz" where it was not preceded by a period ("."). Even though there were no test cases for this scenario, it should not be included since it is possible. I was just trying to find a method other than using regular expressions (although \\.xyz is a regular expression) that uses a whole new level to your learning process. It would be possible to search for a preceding "x" and following "yz" or a preceding "xy" and a following "z" but, that in all likelihood require additional regular expression syntax.
Forgive my typos -
The same would hold true if there were "xy" before ".xyz"
The same would hold true if there were "xy" before ".xyz"
ASKER
str.replaceAll("\\.xyz", "").==> this simply removes all chase
why we need two \\
please advise
one \ will escape . that is fine
but what other \ does here?
ASKER
if the .xyz was originally preceded by an "x" and followed by "yz" it would create a "xyz" sequence without the dot and return true when the original string had no "xyz" without a dot in front and should have returned false. The same would hold true if there were "xy" before ".xyx" with a "z" afterwards.in challenge where it said above shoudl return false?? how did we infer above?
Return true if the given string contains an appearance of "xyz" where the xyz is not directly preceeded by a period (.). So "xxyz" counts but "x.xyz" does not.
xyzThere("abcxyz") → true
xyzThere("abc.xyz") → false
xyzThere("xyz.abc") → true
ASKER
return str.matches("(.*[^.])?xyz.*");
can you please explain above in simple words so that my tiny mind can digest it?
It would be possible to search for a preceding "x" and following "yz" or a preceding "xy" and a following "z" but, that in all likelihood require additional regular expression syntax.That's unnecessary. It's much simpler just to replace "\\.xyz" with something not contained in "xyz" instead of with ""
(as done here http:#a41452857)
what other \ does here?In a Java string literal, it escapes the second \ to include a real \ in the string
http://docs.oracle.com/javase/tutorial/java/data/characters.html
where it said above shoudl return false?? how did we infer above?It doesn't explicitly say you should return false, it only says when you should return true. You infer that it wants you to return false when it does not tell you to return true.
(otherwise you could satisfy the challenge by always returning true, with would be a less interesting challenge)
please explainAs you seem to like regex101.com explanations:
https://regex101.com/r/rE7bD4/6
(although str.matches works a little differently from regex101.com in that str.matches requires that the entire string is matched, while regex101.com allows matching on part of the string)
>>That's unnecessary. It's much simpler just to replace "\\.xyz" with something not contained in "xyz" instead of with ""<<
DOH! I agree. I originally replaced it with "AAAA" why I changed it, I don't know.
DOH! I agree. I originally replaced it with "AAAA" why I changed it, I don't know.
ASKER
\\ Insert a backslash character in the text at this point.
http://docs.oracle.com/javase/tutorial/java/data/characters.html
why we need backslash here? to escape . right?
https://regex101.com/r/rE7bD4/6
i wonder how above site remembers what you typed so that wheni click it does not show default home page or default values but keeping in some session.
anyway i am trying to understand it now
>>That's unnecessary. It's much simpler just to replace "\\.xyz" with something not contained in "xyz" instead of with ""<<
DOH! I agree. I originally replaced it with "AAAA" why I changed it, I don't know.
i lost what you are discussing here.
we thought of replacing with "" but that is not good approach instead replace with some other string?
ASKER
(.*[^.])?
why we dicided to have first capturing group(say x as highlighted) for zero or 1 only not many?
x?
above is zero or one of x
also why we used start character twice as ^
and end character $ at the end after *(i think if * comes after $ then only it has impact on $ other wise no impact on last character right)
why we dicided to have first capturing group(say x as highlighted) for zero or 1 only not many?
x?
above is zero or one of x
also why we used start character twice as ^
and end character $ at the end after *(i think if * comes after $ then only it has impact on $ other wise no impact on last character right)
ASKER
It doesn't explicitly say you should return false, it only says when you should return true. You infer that it wants you to return false when it does not tell you to return true.
(otherwise you could satisfy the challenge by always returning true, with would be a less interesting challenge)
i do not think this way of backward thinking usually which i think i should develop.how to develop this kind of backward thinking of reading between the words for implicit meanings?
ASKER
public boolean xyzThere(String str) {
return str.replaceAll("\\.xyz", "").indexOf("xyz") >0;
}
above failed 2 tests cases when xyz came at beginning which make sense to me. is there is string function which checks presense of givven string say xyz anywhere and gives positive number like +1 similar to how it gives -1 if it does not contain ?
ASKER
It would be possible to search for a preceding "x" and following "yz" or a preceding "xy" and a following "z" but, that in all likelihood require additional regular expression syntax.
That's unnecessary. It's much simpler just to replace "\\.xyz" with something not contained in "xyz" instead of with ""
(as done here http:#a41452857)
not 100% clear on above only 70% i got
ASKER
http://www.tutorialspoint.com/compile_java_online.php
abovve gave true
public class HelloWorld{
public static void main(String []args){
System.out.println("Hello World"+xyzThere("xxyzyz"));
}
public static boolean xyzThere(String str) {
return str.replaceAll("\\.xyz",".").indexOf("xyz")>=0;
}
}
abovve gave true
ASKER
public class HelloWorld{
public static void main(String []args){
System.out.println("Hello World"+xyzThere("xxyzyz"));
}
public static boolean xyzThere(String str) {
return str.replaceAll("\\.xyz","").indexOf("xyz")>=0;
}
}
above also gave true
ASKER
public class HelloWorld{
public static void main(String []args){
System.out.println("Hello World"+xyzThere("xxyzyz"));
}
public static boolean xyzThere(String str) {
return str.replaceAll("\\.xyz","AAA").indexOf("xyz")>=0;
}
}
above also gave true.
not sure how "" and "." and "AAA" make difference in output with respect to preceding x and succeeeding yz etc?
by thhe way i like contains() method which i am looking here
ID: 41452857
ASKER
public boolean xyzThere(String str) {
return str.matches("(.*[^.])?xyz.*");
}
how above different from
public boolean xyzThere(String str) {
return str.matches("(.*[^.])?xyz.
}
ASKER
public boolean xyzThere(String str) {
String strNew="xyz";
String strDot=".xyz";
String strModified=str.replace(strDot,"aaa");
if(strModified.contains(strNew)){
return true;
}
else
return false;
}
as above i fixed my original approach similar to above posts. please advise any improvementss?
ASKER
https://regex101.com/r/rE7bD4/6
in above site what is meaning of substitution string shown at bottom after Regular Esxpression and Test String section ?
how to make changes and submit.
when i click enter sometimes going to next line (but not executing the regular expression?)
in above site what is meaning of substitution string shown at bottom after Regular Esxpression and Test String section ?
how to make changes and submit.
when i click enter sometimes going to next line (but not executing the regular expression?)
how above differentIt is not different. matches requires that the match be anchored regardless of whether you explicitly include the anchor
what is meaning of substitution stringWhen you use matches, no substitution is done, and you only care whether the match succeeds or fails. regex101 allows the option to do a substitution, so here it was just used help demonstrate where and how the match happens, even though the substitutions would have no effect on this challenge.
ASKER
how above different
It is not different. matches requires that the match be anchored regardless of whether you explicitly include the anchor
what is meaning of anchor and anchored. i am not following this word. please advise
When you use matches, no substitution is done, and you only care whether the match succeeds or fails. regex101 allows the option to do a substitution, so here it was just used help demonstrate where and how the match happens, even though the substitutions would have no effect on this challenge.how you rerun after making some change in regex101 site? any tutorial videos on this?
ASKER
how do you read above?
str can optionally start with a string of characters ending in a character other than '.'
followed by "xyz"
followed by anything
e.g. "abc.xyzxyz"
(.*[^.])?xyz.* matches here "abc.xyzxyz"
(.*[^.])?xyz.* matches here "abc.xyzxyz"
(.*[^.])?xyz.* matches here "abc.xyzxyz"
(.*[^.])?xyz.* matches here "abc.xyzxyz"
(.*[^.])?xyz.* matches "abc.xyzxyz<here>"
(.*[^.])?xyz.* matches "abc.xyzxyz"
"xyz.abc"
(.*[^.])?xyz.* matches "<here>xyz.abc"
(.*[^.])?xyz.* matches here "xyz.abc"
(.*[^.])?xyz.* matches here "xyz.abc"
(.*[^.])?xyz.* matches "xyz.abc"
i still did not understand above?
why it comes and stops at what point and regex covers which part of string?
please advise
str can optionally start with a string of characters ending in a character other than '.'
followed by "xyz"
followed by anything
e.g. "abc.xyzxyz"
(.*[^.])?xyz.* matches here "abc.xyzxyz"
(.*[^.])?xyz.* matches here "abc.xyzxyz"
(.*[^.])?xyz.* matches here "abc.xyzxyz"
(.*[^.])?xyz.* matches here "abc.xyzxyz"
(.*[^.])?xyz.* matches "abc.xyzxyz<here>"
(.*[^.])?xyz.* matches "abc.xyzxyz"
"xyz.abc"
(.*[^.])?xyz.* matches "<here>xyz.abc"
(.*[^.])?xyz.* matches here "xyz.abc"
(.*[^.])?xyz.* matches here "xyz.abc"
(.*[^.])?xyz.* matches "xyz.abc"
i still did not understand above?
why it comes and stops at what point and regex covers which part of string?
please advise
I tried to use underlines and bold text in earlier comments to illustrate what parts of the regex matches which part of the string, but do not seem to have succeeded in making myself clear. regex101 uses color to illustrate the same thing, so perhaps experimenting there would explain better than I can.
ASKER
regex101 uses color to illustrate the same thing, so perhaps experimenting there would explain better than I can.i am not able to experiment there as i was not sure how to modify to experiment and execute new regex to see new output.
please advise
ASKER
https://regex101.com/r/rE7bD4/6
how java ? operator is different from regex ? operator. I was not clear on that?(i know java ? but not regex ?)
why we got output like below
abc.xyz xyz true
i expected as below instead similar to others
abc.xyzxyz
xyz true
below operators are also not clear when and how to use them
^(this is to tell regex that this is first character??)
$(this is to tell regex this is last charactr??)
gm not sure what gm in regex101 site means?
how java ? operator is different from regex ? operator. I was not clear on that?(i know java ? but not regex ?)
why we got output like below
abc.xyz xyz true
i expected as below instead similar to others
abc.xyzxyz
xyz true
below operators are also not clear when and how to use them
^(this is to tell regex that this is first character??)
$(this is to tell regex this is last charactr??)
gm not sure what gm in regex101 site means?
ASKER
how $1 is different from $2 or $3 in the substitution?
when i say $2 then no output coming
when i say $2 then no output coming
what gm in regex101 site means?Hover over the ? in the input box where the modifiers were entered.
(these modifiers did not apply to the original Java regular expression, they were only used here to be able to show several different examples of there the regular expression could match)
how $1 is different from $2 or $3 in the substitution$1 gets replaced with the contents of capture group 1,
$2 gets replaced with the contents of capture group 2,
$3 gets replaced with the contents of capture group 3
$2 then no outputThe regular expression had only 1 pair of parentheses
ASKER
how $1 is different from $2 or $3 in the substitution
$1 gets replaced with the contents of capture group 1,
$2 gets replaced with the contents of capture group 2,
$3 gets replaced with the contents of capture group 3
$2 then no output
The regular expression had only 1 pair of parentheses
how to know how many capture groups to use. I feel more capture groups better and easy to understand to begineers?
Is that is purpose of capture group?
ASKER
what gm in regex101 site means?
Hover over the ? in the input box where the modifiers were entered.
(these modifiers did not apply to the original Java regular expression, they were only used here to be able to show several different examples of there the regular expression could match)
when i hower on ? says zero or one or as many
not sure what that confusing working means as attached.
hower.png
how many capture groups to useAs many as you need.
purpose of capture group?Two purposes, grouping and capturing.
You can group without capturing with a (?:non-capturing group)
hower on ? says zero or oneNot that ?
You were asking about gm so hover your cursor on the white ? in the grey circle in the box where the gm was entered
But congratulations on figuring out how to answer your question about regex ? operator
ASKER
But congratulations on figuring out how to answer your question about regex ? operatori am not clear on 0 or 1 or as many times? what is allowed with ?
any simple examples using regex ? within java program
i see as attached lot of description about gm
hower2.png
^(.*[^.])?xyz.*$
(.*[^.]) 0 times
^xyz.*$
(.*[^.]) 1 times
^(.*[^.])xyz.*$
so
^(.*[^.])?xyz.*$ matches anything that is matched by either ^xyz.*$ or ^(.*[^.])xyz.*$
(.*[^.]) 0 times
^xyz.*$
(.*[^.]) 1 times
^(.*[^.])xyz.*$
so
^(.*[^.])?xyz.*$ matches anything that is matched by either ^xyz.*$ or ^(.*[^.])xyz.*$
ASKER
xyzThere("abc.xyzxyz") → counts
xyzThere("abc.xyzxyz") → does not
i am still not clear on one point why above does not count based on below description
Return true if the given string contains an appearance of "xyz" where the xyz is not directly preceeded by a period (.). So "xxyz" counts but "x.xyz" does not.
xyzThere("abcxyz") → true
xyzThere("abc.xyz") → false
xyzThere("xyz.abc") → true
please advise
ASKER
xyzThere("abc.xyzxyz") → does not
above satisfies below two conditions right
1. Return true if the given string contains an appearance of "xyz" where
2. the xyz is not directly preceeded by a period (.).
so point 2 overrides point 1?
as it has two disting strings like
A)xyz
and also
B).xyz
so we have to decide based on A or based on B above?
above satisfies below two conditions right
1. Return true if the given string contains an appearance of "xyz" where
2. the xyz is not directly preceeded by a period (.).
so point 2 overrides point 1?
as it has two disting strings like
A)xyz
and also
B).xyz
so we have to decide based on A or based on B above?
I did not say
xyzThere("abc.xyzxyz") → does not
I said
xyzThere("abc.xyzxyz") → does not
[b]Highlighting[/b] the part that does not, in order to contrast it with the part that does.
https://www.experts-exchange.com/questions/28921303/xyzThere-java-challenge.html?anchorAnswerId=41440415#a41440415
xyzThere("abc.xyzxyz") → does not
I said
xyzThere("abc.xyzxyz") → does not
[b]Highlighting[/b] the part that does not, in order to contrast it with the part that does.
https://www.experts-exchange.com/questions/28921303/xyzThere-java-challenge.html?anchorAnswerId=41440415#a41440415
ASKER
I did not say
xyzThere("abc.xyzxyz") → does not
I said
xyzThere("abc.xyzxyz") → does not
Highlighting the part that does not, in order to contrast it with the part that does.
https://www.experts-exchange.com/questions/28921303/xyzThere-java-challenge.html?anchorAnswerId=41440415#a41440415
But we have to see complete method with its complete arument which in this case
xyzThere("abc.xyzxyz")
to see it does(true) or does not(false) right?
xyzThere("abc.xyzxyz")
i was thinking above has false case and true case both in it so combined result is true or false?
i do not see challenge talked about combined case
Return true if the given string contains an appearance of "xyz" where the xyz is not directly preceeded by a period (.). So "xxyz" counts but "x.xyz" does not.
xyzThere("abcxyz") → true
xyzThere("abc.xyz") → false
xyzThere("xyz.abc") → true
except that result shows it should be true
xyzThere("abc.xyzxyz") → true
But one can argue it could be false also right?
please advise
xyzThere("abc.xyzxyz") → does not
I said
xyzThere("abc.xyzxyz") → does not
Highlighting the part that does not, in order to contrast it with the part that does.
https://www.experts-exchange.com/questions/28921303/xyzThere-java-challenge.html?anchorAnswerId=41440415#a41440415
But we have to see complete method with its complete arument which in this case
xyzThere("abc.xyzxyz")
to see it does(true) or does not(false) right?
xyzThere("abc.xyzxyz")
i was thinking above has false case and true case both in it so combined result is true or false?
i do not see challenge talked about combined case
Return true if the given string contains an appearance of "xyz" where the xyz is not directly preceeded by a period (.). So "xxyz" counts but "x.xyz" does not.
xyzThere("abcxyz") → true
xyzThere("abc.xyz") → false
xyzThere("xyz.abc") → true
except that result shows it should be true
xyzThere("abc.xyzxyz") → true
But one can argue it could be false also right?
please advise
No,
one can argue that
xyzThere(".xyz") → false
but
xyzThere("abc.xyzxyz") is not xyzThere(".xyz")
There are many parts of "abc.xyzxyz" which do not satisfy "an appearance of "xyz" where the xyz is not directly preceeded by a period (.)"
none of "a", "b", "c", ".", "x", "y", "z", "x", "y", "z" satisfy, but all together "abc.xyzxyz" does.
Which is why it is not sufficient to just look at a single part to determine the result.
one can argue that
xyzThere(".xyz") → false
but
xyzThere("abc.xyzxyz") is not xyzThere(".xyz")
There are many parts of "abc.xyzxyz" which do not satisfy "an appearance of "xyz" where the xyz is not directly preceeded by a period (.)"
none of "a", "b", "c", ".", "x", "y", "z", "x", "y", "z" satisfy, but all together "abc.xyzxyz" does.
Which is why it is not sufficient to just look at a single part to determine the result.
ASKER
but all together "abc.xyzxyz" does.but according to challenge if it does precede by . it should be give result false as a whole right?
but why it is giving true as below
xyzThere("abc.xyzxyz") → true true OK
i am expecting as below
xyzThere("abc.xyzxyz") → false false
public boolean xyzThere(String str) {
for( int i=0;i<=str.length(); i++ ){
if(str.substring(0,i).endsWith("xyz")&&!str.substring(0,i).endsWith(".xyz")
){
return true;
}
}
return false;
}
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
The fact that it also contains an appearance of xyz that is preceded by a period is moot.
based on order of sentences that are given in challenge right. First sentence has more importance and second one goes to moot right if first and second are contradictory to each other?
First and second are not contradictory.
Return true if the given string contains an appearance of "X"
So "YX" counts but "AB" does not.
Does the second sentence say anything about whether "ABYX" contains an appearance of "X"?
Return true if the given string contains an appearance of "X"
So "YX" counts but "AB" does not.
Does the second sentence say anything about whether "ABYX" contains an appearance of "X"?
ASKER
Return true if the given string contains an appearance of "xyz" where the xyz is not directly preceeded by a period (.). So "xxyz" counts but "x.xyz" does not.
xyzThere("abcxyz") → true
xyzThere("abc.xyz") → false--->it this is false how xyzThere("abc.xyzxyz") is true as it has .xyz?
xyzThere("xyz.abc") → true
xyzThere("abcxyz") → true
xyzThere("abc.xyz") → false--->it this is false how xyzThere("abc.xyzxyz") is true as it has .xyz?
xyzThere("xyz.abc") → true
"x.xyz" and "abc.xyz" do not contain an appearance of "xyz" where the xyz is not directly prece[e]ded by a period
"xxyz" and "abc.xyzxyz" do contain an appearance of "xyz" where the xyz is not directly prece[e]ded by a period
Perhaps you are misreading the first sentence as
Return true if the given string contains an appearance of "xyz" where no xyz is directly prece[e]ded by a period (.).
or
Return true if the given string contains an appearance of "xyz" where all xyz are not directly prece[e]ded by a period (.).
instead of
Return true if the given string contains an appearance of "xyz" where the xyz is not directly prece[e]ded by a period (.).
"xxyz" and "abc.xyzxyz" do contain an appearance of "xyz" where the xyz is not directly prece[e]ded by a period
Perhaps you are misreading the first sentence as
Return true if the given string contains an appearance of "xyz" where no xyz is directly prece[e]ded by a period (.).
or
Return true if the given string contains an appearance of "xyz" where all xyz are not directly prece[e]ded by a period (.).
instead of
Return true if the given string contains an appearance of "xyz" where the xyz is not directly prece[e]ded by a period (.).
ASKER
or
Return true if the given string contains an appearance of "xyz" where all xyz are not directly prece[e]ded by a period (.).
i am thinking exactly as above
instead of
Return true if the given string contains an appearance of "xyz" where the xyz is not directly prece[e]ded by a period (.).
you mean as below
instead of
Return true if the given string contains an appearance of "xyz" where the same xyz is not directly prece[e]ded by a period (.).
the same
seems a less awkward interpretation than
the other
seems a less awkward interpretation than
the other
ASKER
the other
which other you are referrring.
Looks like challenge gives false if only one xyz is there and if it preceded by dot (like xyzThere("abc.xyz") → false)
if more than one xyz say 2 xyz's(like abc.xyzxyz) first xyz is preceded by dot and second xyz is not preceded by dot then it is true
if more than one xyz say 2 xyz's(like abc.xyzxyzxyz) first xyz is preceded by dot and second, third xyz are not preceded by dot then it is true
i think challenge should say as below to be more clear
Return true if the given string contains an atleast one appearance of "xyz" where the that xyz is not directly preceeded by a period (.). So "xxyz" counts but "x.xyz" does not.
i think challenge should say as below to be more clearPerhaps.
Return true if the given string contains an at least one appearance of
If so, it may be more useful to discuss that on http://codingbat.com than on https://www.experts-exchange.com
But "contains" usually means the same as "contains an at least one"
ASKER
But "contains" usually means the same as "contains an at least one"
ok got you. Thank you very much for your help for making me understand this intrinsic challenge
To be more clear to me
Return true if the given string contains an appearance of "xyz" where the xyz is not directly preceeded by a period (.). So "xxyz" counts but "x.xyz" does not.
may be like this is more clear to me.
Return true if the given string contains atleast one appearance of "xyz" other than .xyz if present. So "xxyz" counts but "x.xyz" does not.
ASKER
Open in new window
return str.matches("(.*[^.])?xyz.
what is logic behind above line
for any character in str we are checking no . should come it that is true then xyz comes??