开发者

question about prefix and suffix in java

I have an issue and i want your help!I have a specific REGEX which i have named it "unique" and i want to store the data that it gives me!! The prefix and the suffix of this regex!!so!!someone has told me to use

var prefix and

var suffix

but i don't know how to do it!

for your convinience i give u my xml for this regex

<reg name="unique">
<start><![CDATA[ <!-- 72 HOURS FORECASTS -->
]]></start>
<end><![CDATA[<!-- 72 HOURS FORECASTS -->]]></end>
</reg>

the code where i call this regex is

int k = 0;

for(Xml reg_is:fetchsite.child("site").child("regexps").children("reg")) {
    if(reg_is.string("name").contains("unique")){


        if(reg_开发者_开发百科is.child("start").content()=="")
            error += "\tNo prefix reg.exp. given.\n";

        else
              prefix = HtmlMethods.removeBreaks(replaceVariables(reg_is.child("start").content()));                     

        if(reg_is.child("end").content()=="")
            error += "\tNo suffix reg.exp. given.\n";
        else
                suffix = HtmlMethods.removeBreaks(replaceVariables(reg_is.child("end").content()));

    }
    else{
          poleis[k][0]= HtmlMethods.removeBreaks(reg_is.string("name"));
          poleis[k][1] = HtmlMethods.removeBreaks(replaceVariables(reg_is.child("start").content())); 
          poleis[k][2] = HtmlMethods.removeBreaks(replaceVariables(reg_is.child("end").content()));

           k++;
 }


Some general comments on your code:

  • Using == to check for String equality is wrong! Use the equals() method instead:

    reg_is.child("end").content().equals("")
    
  • Also, if you're comparing a String variable to a hard-coded String, it's better to put the hard-coded String first. That way, if the variable is null, your program won't throw a NullPointerException (because the hard coded String can never be null!)

    "".equals(reg_is.child("end").content())
    
  • When doing a lot of String concatenation, it's more efficient to use a StringBuilder:

     StringBuilder error = new StringBuilder();
     error.append("\tNo prefix reg.exp. given.\n");
    
  • Like Oscar Reyes said, the safe thing to do with if statements is always use braces. If you add a line and forget to add the braces, you can waste hours tracking down bugs. I know it's happened to me before...

    if(reg_is.child("start").content()==""){
      error += "\tNo prefix reg.exp. given.\n";
    } else {
      prefix = HtmlMethods.removeBreaks(replaceVariables(reg_is.child("start").content()));
    }
    
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜