我需要一个由一些数字,逗号,下划线,斜线,连字符和空格组成的字符串。
我差点儿好。到目前为止,这是我的代码。
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
<classpathentry kind="lib" path="lib/cta-junit-runner-0.0.1-SNAPSHOT-jar-with-dependencies.jar"/>
<classpathentry kind="lib" path="lib/cta-junit-runner-0.0.1-SNAPSHOT.jar"/>
<classpathentry kind="lib" path="lib/junit-4.12.jar"/>
<classpathentry kind="lib" path="lib/hamcrest-core-1.3.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>
但我希望在0到9之间至少包含4位数。
例如,这些是我想要有效的值:
public class SchoolController : Controller
{
private SchoolContext db = new SchoolContext();
public ActionResult Contact()
{
int ID = TempData["SchoolID"];
// I fetch school from the database
SchoolModel school = db.School.Find(ID);
//and I map the properties to the ViewModel
ContactViewModel contact = new ContactViewModel();
contact.Address = school.Address;
contact.Phone = school.Phone;
//etc.
return View(contact);
}
}
public class OtherController : Controller
{
public ActionResult School(int id)
{
TempData["SchoolID"] = id;
return RedirectToAction("/School/Contact");
}
}
这些是我不想要有效的值:
$regex = '#^[0-9-_,/\s]+$#';
if(preg_match($regex, $string)){
return true;
} else {
return false;
}
所以,我试过这样。
09-12345678
123, 456, 789
12/ 34/ 56
和
1,2
3/5/6
7-8-9
但他们也不是真的。 我该怎么办?
答案 0 :(得分:2)
您可以使用基于前瞻性的正则表达式:
~^(?=(?:\D*\d){4})[-0-9_,/\s]+$~
(?=(?:\D*\d){4})
预示输入中至少存在4位数字。[...]