我不知道如何从字符串中提取具有特定匹配项的特定数字。
例:
string myString = "blah blah **[10]** blah **[20]** and some more blah **[30]**";
Regex myIDsReg = new Regex(@"\*\*\[(\d+)\]\*\*");
显然正则表达式是声音。
Match myMatch = myIDsReg.Match(myString);
产生“ ** [10] **”,但仅此而已。
我不知道如何获取具有以下值的数组:10、20、30
参考方案
我会这样做
string myString = "blah blah **[10]** blah **[20]** and some more blah **[30]**";
Regex myIDsReg = new Regex(@"\*\*\[(\d+)\]\*\*");
string[] regexResult = (from Match match in myIDsReg.Matches(myString) select match.Groups[1].Value).ToArray();
您也可以选择想要的输出
List<string> regexResult = (from Match match in myIDsReg.Matches(myString) select match.Groups[1].Value).ToList();
要么
IEnumerable<string> regexResult = (from Match match in myIDsReg.Matches(myString) select match.Groups[1].Value);
我更喜欢后者的两个之一
用大写字母拆分字符串,但忽略AAA Python Regex - python我的正则表达式:vendor = "MyNameIsJoe. I'mWorkerInAAAinc." ven = re.split(r'(?<=[a-z])[A-Z]|[A-Z](?=[a-z])', vendor) 以大写字母分割字符串,例如:'我的名字是乔。 I'mWorkerInAAAinc”变成…
当回复有时是一个对象有时是一个数组时,如何在使用改造时解析JSON回复? - java我正在使用Retrofit来获取JSON答复。这是我实施的一部分-@GET("/api/report/list") Observable<Bills> listBill(@Query("employee_id") String employeeID); 而条例草案类是-public static class…
Mongo汇总 - javascript我的收藏中有以下文件{ "_id": ObjectId("54490b8104f7142f22ecc97f"), "title": "Sample1", "slug": "samplenews", "cat": …
如何在Wiremock中为JUNIT匹配精确的json - java我正在使用Wiremock在Spring启动应用程序中模拟Junit的REST服务。我的问题是,我无法匹配多个匹配模式。 Junit.javaStringValuePattern pattern = WireMock.matching(".*"); givenThat(post(urlEqualTo("/softwares…
如何在JQuery中操作JSONArray - javascript我有一个php函数,它以JSON返回此代码{"0":{"title":"Dans l\u2019appartement"},"1":{"title":"A l\u2019a\u00e9roport - D\u00e9part de B\u00e9at…