正则表达式中的数字没有相邻的数字

时间:2018-07-24 22:25:54

标签: regex

我想让正则表达式找到一个特定的数字。例如

这些应该是真的

99 Hello
Hello 99 Hello
Hello 99
(99 Hello)
99 Hello 999

这应该是错误的

999 Hello

如您所见,我想找到任何出现的数字99,但要确保它没有相邻的数字。这似乎很容易,但是我还没有找到完全满足我要求的东西。

编辑: 我尝试了一些关于stackoverflow的不同建议。不幸的是,我没有跟踪它们。会议结束后,我是否可以再次找到它们,所以我需要去这里发布。通常,我会在[?0-9]之前和之后进行类似99的实验,以确保我需要的数字的邻居不是数字。不幸的是,如果import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Intent; import android.support.v4.app.NotificationCompat; import com.google.firebase.messaging.RemoteMessage; public class FirebaseMessagingService extends com.google.firebase.messaging.FirebaseMessagingService { @Override public void onMessageReceived(RemoteMessage remoteMessage) { super.onMessageReceived(remoteMessage); String notification_title = remoteMessage.getNotification().getTitle(); String notification_message = remoteMessage.getNotification().getBody(); String click_action = remoteMessage.getNotification().getClickAction(); String from_user_id = remoteMessage.getData().get("from_user_id"); NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(FirebaseMessagingService.this, "default") .setSmallIcon(R.mipmap.ic_launcher) .setContentTitle(notification_title) .setContentText(notification_message); Intent resultIntent = new Intent(click_action); resultIntent.putExtra("user_id", from_user_id); PendingIntent resultPendingIntent = PendingIntent.getActivity( FirebaseMessagingService.this, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT ); mBuilder.setContentIntent(resultPendingIntent); int mNotificationId = (int) System.currentTimeMillis(); NotificationManager mNotifyMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); mNotifyMgr.notify(mNotificationId, mBuilder.build()); } } 在行的开头或结尾,则该方法将失败。

我尝试了这些,但它们未通过测试     [?0-9] {0,1} 99 [?0-9] {0,1}

1 个答案:

答案 0 :(得分:1)

Regex101非常适合这类事情。

\ b99 \ b可以解决问题。 \ b是空格,linestart或lineend。