Note that there are some explanatory texts on larger screens.

plurals
  1. POWhich is the best way to match short string? regular expression or if else statement?
    primarykey
    data
    text
    <p>In testOne() I use regex expression use judge if a string contains some specific strings</p> <p>In testTwo() I use if else statement to to the same thing</p> <p>I wonder why testTwo() is always faster than testOne() in my test cases</p> <p>Is regex expression not suitable for the problem? or my regex expression is not well written?</p> <p>My test code is as follow, thanks very much!</p> <pre><code>public class TestReg { static final Pattern PATT = Pattern .compile("(tudou|video.sina|v.youku|v.ku6|tv.sohu|v.163|tv.letv|v.ifeng|v.qq|iqiyi|(5)?6)\\.(com|cn)"); @Test public void testOne() { int count = 0; for (int i = 0; i &lt; 10000; i++) { for (String vurl : TESTCASES) { if (PATT.matcher(vurl).find()) count++; } } System.out.println("testOne:" + count); } @Test public void testTwo() { int count = 0; for (int i = 0; i &lt; 10000; i++) { for (String vurl : TESTCASES) { if (vurl.indexOf("tudou.com") != -1 || vurl.indexOf("video.sina.com") != -1 || vurl.indexOf("v.youku.com") != -1 || vurl.indexOf("v.ku6.com") != -1 || vurl.indexOf("56.com") != -1 || vurl.indexOf("tv.sohu.com") != -1 || vurl.indexOf("v.163.com") != -1 || vurl.indexOf("tv.letv.com") != -1 || vurl.indexOf("v.ifeng.com") != -1 || vurl.indexOf("v.qq.com") != -1 || vurl.indexOf("iqiyi.com") != -1 || vurl.indexOf("6.cn") != -1) { count++; } } } System.out.println("testOne:" + count); } static final String[] TESTCASES = { "http://blog.csdn.net/v_july_v/article/details/7624837", "http://jobs.douban.com/intern/apply/?type=dev&amp;position=intern_sf", "https://class.coursera.org/ml/lecture/index", "http://blog.csdn.net/v_july_v/article/details/7624837", "http://jobs.douban.com/intern/apply/?type=dev&amp;position=intern_sf", "https://class.coursera.org/ml/lecture/index", "http://blog.csdn.net/v_july_v/article/details/7624837", "http://jobs.douban.com/intern/apply/?type=dev&amp;position=intern_sf", "https://class.coursera.org/ml/lecture/index", "http://blog.csdn.net/v_july_v/article/details/7624837", "http://jobs.douban.com/intern/apply/?type=dev&amp;position=intern_sf", "https://class.coursera.org/ml/lecture/index", "http://www.56.com/u38/v_NjYyNTUyMjc.html", "http://video.sina.com.cn/v/b/69614895-2128825751.html", "http://www.tudou.com/programs/view/xcPewAoJ26M", "http://v.youku.com/v_show/id_XMzQ0OTI0MTgw.html", "http://www.56.com/u87/v_NjMzMjEzNTY.html", "http://tv.sohu/u87/v_NjMzMjEzNTY.html", "http://tv.letv/u38/v_NjYyNTUyMjc.html", "http://v.ifeng/v/b/69614895-2128825751.html", "http://v.qq/programs/view/xcPewAoJ26M", "http://v.163/v_show/id_XMzQ0OTI0MTgw.html", "http://iqiyi/u87/v_NjMzMjEzNTY.html", "http://v.6.cn/u87/v_NjMzMjEzNTY.html" }; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload