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
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;
}
}
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
ASKER
xyzThere("abc.xyzxyz") → counts
xyzThere("abc.xyzxyz") → does not
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
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;
}
}
}
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
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;
}
}
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
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?
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?
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)){
return true;
}
else if(str.contains(strDot)){
return false;
}
else{
return false;
}
}
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
ASKER
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;
}
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
ASKER
xyzThere(".xyz") → false true X
xyzThere("12.xyz") → false true X
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;
}
}
ASKER
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
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;
}
ASKER
return str.matches("
(.*[^.])
?xyz.*"
);
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;
}
ASKER
public boolean xyzThere(String str) {
return str.startsWith("xyz") || str.length()>0 && xyzThere(str.replaceFirst("\\.*.",""));
}
ASKER
how to make my approach work ?Understand the potentials and limits of the approach.
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;
}
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
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;
}
}
ASKER
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
substring(0,1) and substring(0,2) are not necessaryGood observation.
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
ASKER
ASKER
So when both str.contains(strNew) and str.contains(strDot) are true, what does that tell you about xyzThere(str)?
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.
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??
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.
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
ASKER
so str.contains("xyz") and str.contains(".xyz") are not sufficient to distinguish between
xyzThere("abc.xyz") and xyzThere("abc.xyzxyz")
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;
}
ASKER
ASKER
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;
}
}
ASKER
ASKER
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;
}
}
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;
}
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==0ASKER
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
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;
}
}
ASKER
if( str.substring(0,i).endsWith( "xyz")&& !str.substring(0,i).endsWi th(".xyz")
above gave correct output true without i==0??You are correct.
str.substring(0,i).endsWitwith xyzThere("1.xyz.xyz2.xyz")h( "xyz")&& !str.substring(0,i+1).ends With(".xyz ")
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
ASKER
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")
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
why except \n(I presume you meant \n, not \?)
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?str can optionally start with a string of characters ending in a character other than '.'
ASKER
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"
ASKER
ASKER
(.*[^.])?xyz.* matches here "!xyz"
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>"
quotesso it is not clear what parts of the expression or string are being asked about.
ASKER
The bolding and underlineing are not getting copied in the
quotes
ASKER
str.replaceAll("\\.xyz", "").indexOf("xyz") > 0;we are replacing .xyz with empty right
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
ASKER
return str.matches("(.*[^.])?xyz.*");
return str.replaceAll("\\.xyz", "").indexOf("xyz") > 0;
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.
ASKER
Technically, replaceAll("\\.xyz","") does have a regular expression.
str.replaceAll("\\.xyz", "").indexOf("xyz") > 0
ASKER
public boolean xyzThere(String str) {
return str.replaceAll("\\.xyz", "").indexOf("xyz") >= 0;
}
public boolean xyzThere(String str) {
return str.replaceAll("\\.xyz", "").indexOf("xyz") [b][u]=[/u][/b]> 0;
}
ASKER
return str.replaceAll("\\.xyz", "").indexOf("xyz") >= 0;
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 meaning of above lineDid you read the Java documentation?
ASKER
In a Java "quoted" string \
ASKER
return str.replaceAll("\\.xyz", "").indexOf("xyz") >= 0;
ASKER
return str.matches("(.*[^.])?xyz.*");
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
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
ASKER
str.replaceAll("\\.xyz", "").==> this simply removes all chase
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.*");
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 ""
what other \ does here?In a Java string literal, it escapes the second \ to include a real \ in the string
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.
please explainAs you seem to like regex101.com explanations:
ASKER
\\ Insert a backslash character in the text at this point.
http://docs.oracle.com/javase/tutorial/java/data/characters.html
>>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.
ASKER
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)
ASKER
public boolean xyzThere(String str) {
return str.replaceAll("\\.xyz", "").indexOf("xyz") >0;
}
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)
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;
}
}
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;
}
}
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;
}
}
ASKER
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;
}
ASKER
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
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
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.
ASKER
ASKER
what gm in regex101 site means?Hover over the ? in the input box where the modifiers were entered.
how $1 is different from $2 or $3 in the substitution$1 gets replaced with the contents of capture group 1,
$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
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)
how many capture groups to useAs many as you need.
purpose of capture group?Two purposes, grouping and capturing.
hower on ? says zero or oneNot that ?
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 ?
ASKER
xyzThere("abc.xyzxyz") → counts
xyzThere("abc.xyzxyz") → does not
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
ASKER
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?
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
The fact that it also contains an appearance of xyz that is preceded by a period is moot.
ASKER
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 (.).
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 (.).
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 (.).
ASKER
the other
i think challenge should say as below to be more clearPerhaps.
Return true if the given string contains an at least one appearance of
ASKER
But "contains" usually means the same as "contains an at least one"
Java is a platform-independent, object-oriented programming language and run-time environment, designed to have as few implementation dependencies as possible such that developers can write one set of code across all platforms using libraries. Most devices will not run Java natively, and require a run-time component to be installed in order to execute a Java program.
TRUSTED BY
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??