我有以下代码。它按预期工作,但我只想知道我是否可以改变其背后的逻辑,使其看起来更简单,因为我认为它是多余的。
它的工作方式如下:我获取一些数据,然后尝试通过正则表达式传递它。有三种可能的结果:
起初我认为有一种方法可以检查正则表达式是否返回空而不抛出异常,但是(如果我错了,请纠正我)没有这样的事情。 ..这就是我包含try / catch的原因。没有它,如果满足第三种情况,IF会返回一个异常,因为它表示m2[0].Groups[1].Captures[0].Value
超出界限(当然,因为它是空的)。
所以...有什么方法可以让这个外观更加优化? 非常感谢你!
string regexString = @" \.\.\. .*?>(.*?)<\/a>";
MatchCollection m2 = Regex.Matches(myInput, regexString, RegexOptions.Singleline);
try
{
if (m2[0].Groups[1].Captures[0].Value.All(Char.IsDigit) == false)
{
regexString = @"page=.*?"">(.*?)</a>\n.*?<a class=""pagebtn""";
m2 = Regex.Matches(myInput, regexString);
}
}
catch (ArgumentException)
{
regexString = @"page=.*?"">(.*?)</a>\n.*?<a class=""pagebtn""";
m2 = Regex.Matches(myInput, regexString);
}
答案 0 :(得分:2)
你可以用IsMatch检查它,如果它不匹配,试试你的第二个正则表达式,如果没有,没有匹配
$.ajax({
url: "path/to/code/file.php" // change this to your path to your code
}).done(function(data) {
// This is executed after the ajax request is complete
// data should be the updated value. You can also return other data // types e.g. JOSN
$('#counter').text(data);
});
在这种情况下,你真的不需要你的try / catch
答案 1 :(得分:2)
我假设您对问题的更准确描述是您希望解析可能存在或不存在整数的值,但如果Fatal Exception: java.lang.NullPointerException
raw
android.view.View.getHardwareLayer (View.java:10178)
android.view.ViewGroup.drawChild (ViewGroup.java:2863)
android.view.ViewGroup.dispatchDraw (ViewGroup.java:2489)
android.view.View.getDisplayList (View.java:10415)
android.view.ViewGroup.dispatchGetDisplayList (ViewGroup.java:2597)
android.view.View.getDisplayList (View.java:10380)
android.view.ViewGroup.dispatchGetDisplayList (ViewGroup.java:2597)
android.view.View.getDisplayList (View.java:10380)
android.view.ViewGroup.dispatchGetDisplayList (ViewGroup.java:2597)
android.view.View.getDisplayList (View.java:10380)
android.view.ViewGroup.dispatchGetDisplayList (ViewGroup.java:2597)
android.view.View.getDisplayList (View.java:10380)
android.view.ViewGroup.dispatchGetDisplayList (ViewGroup.java:2597)
android.view.View.getDisplayList (View.java:10380)
android.view.ViewGroup.dispatchGetDisplayList (ViewGroup.java:2597)
android.view.View.getDisplayList (View.java:10380)
android.view.ViewGroup.dispatchGetDisplayList (ViewGroup.java:2597)
android.view.View.getDisplayList (View.java:10380)
android.view.ViewGroup.dispatchGetDisplayList (ViewGroup.java:2597)
android.view.View.getDisplayList (View.java:10380)
android.view.HardwareRenderer$GlRenderer.draw (HardwareRenderer.java:899)
android.view.ViewRootImpl.draw (ViewRootImpl.java:1910)
android.view.ViewRootImpl.performTraversals (ViewRootImpl.java:1634)
android.view.ViewRootImpl.handleMessage (ViewRootImpl.java:2442)
android.os.Handler.dispatchMessage (Handler.java:99)
android.os.Looper.loop (Looper.java:137)
android.app.ActivityThread.main (ActivityThread.java:4456)
java.lang.reflect.Method.invokeNative (Method.java)
dalvik.system.NativeStart.main (NativeStart.java)
是/**Called when the user clicks the Send button */
public void sendMessage1(View view){
//example phoneNum[1] = "*556";
String encodedHarsh = Uri.encode("#");
startActivity(new Intent
("android.intent.action.DIAL",
Uri.parse("tel:"+ phoneNum[1]+ encodedHarsh)));
//Do something in response to button
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mtn);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_mtn, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
,则m2[0].Groups[1].Captures[0].Value.All(Char.IsDigit)
会抛出Value
。
然后可以将其简化为:
null