matcher

Java正则表达式字符串匹配示例

我的未来我决定 提交于 2020-02-29 23:17:08
一位以前的同事在群里面突然发了个需求,要通过正则表达式来取值。给我发过来一张图, 从图中可以出,需求是,通过下面的正则表达式,取出红色框所标示的内容 开始理解错误,我以为是要取出中间的那些内容,不包括"[标题BEGIN]"和“[标题END]”,于是写了下面的代码: private static void getStr(){ String str="[标题BEGIN]<#list>[@cms_chanel id=70]<li class=\"current\"></li>[/@cms_chanel]</#list>[标题END]"; Pattern p =Pattern.compile("\\[标题BEGIN\\](.*)\\[标题END\\]"); Matcher m =p.matcher(str); ArrayList<String> matches= new ArrayList<String>(); while(m.find()) { matches.add(m.group(1)); } for(String s: matches) { System.out.println("匹配结果:"+s); } } 回过头来一看需求,发现不对,于是再修改,主要是针对pattern进行修改 过程如下(按顺序); 1. Pattern p =Pattern.compile("(\\

Why doesn't this code attempting to use Hamcrest's hasItems compile?

纵饮孤独 提交于 2020-01-19 03:08:48
问题 Why does this not compile, oh, what to do? import static org.junit.Assert.assertThat; import static org.junit.matchers.JUnitMatchers.hasItems; ArrayList<Integer> actual = new ArrayList<Integer>(); ArrayList<Integer> expected = new ArrayList<Integer>(); actual.add(1); expected.add(2); assertThat(actual, hasItems(expected)); error copied from comment: cannot find symbol method assertThat(java.util.ArrayList<java.lang.Integer>, org.hamcreset.Matcher<java.lang.Iterable<java.util.ArrayList<java

Splitting comma separated string, ignore commas in quotes, but allow strings with one double quotation

感情迁移 提交于 2020-01-16 01:07:11
问题 I have searched through several posts on stackoverflow on how to split a string on comma delimiter, but ignore splitting on comma in quotes (see: How do I split a string into an array by comma but ignore commas inside double quotes?) I am trying to achieve just similar results, but need to also allow for a string that contains one double quote. IE. Need "test05, \"test, 05\", test\", test 05" to splits into test05 "test, 05" test" test 05 I tried a similar method to one mentioned here: Regex

Java regex matcher always returns false

自作多情 提交于 2020-01-15 12:24:50
问题 I have a string expression from which I need to get some values. The string is as follows #min({(((fields['example6'].value + fields['example5'].value) * ((fields['example1'].value*5)+fields['example2'].value+fields['example3'].value-fields['example4'].value)) * 0.15),15,9.087}) From this stribg, I need to obtain a string array list which contains the values such as "example1", "example2" and so on. I have a Java method which looks like this: String regex = "/fields\\[['\"]([\\w\\s]+)['\"]\\]

Scala Spec2 Mockito: Argument matchers with complex types

泪湿孤枕 提交于 2020-01-13 23:29:34
问题 I'm trying to write a mock for a web service with Mockito. The mock should simulate a POST request using the play WS library. /** * Mock for the Web Service */ case class WSMock() extends Mockito { val wsRequestHolder: play.api.libs.ws.WS.WSRequestHolder = mock[play.api.libs.ws.WS.WSRequestHolder] val wsResponse: play.api.libs.ws.Response = mock[play.api.libs.ws.Response] wsResponse.status returns 200 wsResponse.body returns "BODY RESP FROM WS" val futureResponse = scala.concurrent.Future {

Matching decimals in strings using matcher()

◇◆丶佛笑我妖孽 提交于 2020-01-11 11:51:29
问题 I have a question regarding the matcher. Currently I am trying to read a string and store all the digits into an array. My question is, how do you try to match both integers and decimals? I have an array of doubles called: double[] thisArray = new double[20]; Into this array, i am trying to store all the numbers I extract from the string. Matcher temp = Pattern.compile("(\d+)").matcher(x); That is my function for the matcher. But this only matches integers. I want to match both integers and

javascript create case insensitive regex from string

ⅰ亾dé卋堺 提交于 2020-01-04 15:50:37
问题 I am trying to do validations by matching inputs to regexes in a case insensitive way. The regex comes down from service as a string on an object. I might get something like: {regex:"ane"} I can do the following: var rx = new RegExp(object.regex); /*The regex is now: /ane/*/ "plane".match(rx); However, I what I really want to do is the following: var rxInsensitive = new RegExp(/ane/i); /*The regex is now: /ane/i */ "plANE".match(rx); I am having problems converting the string to this form

Java - Regex for Full Name

倾然丶 夕夏残阳落幕 提交于 2019-12-31 10:52:33
问题 How can I validate regex for full name? I only want alphabets (no numericals) and only spaces for the regex. This is what I have done so far. Would you please help me fix the regex? Thank you very much public static boolean isFullname(String str) { boolean isValid = false; String expression = "^[a-zA-Z][ ]*$"; //I know this one is wrong for sure >,< CharSequence inputStr = str; Pattern pattern = Pattern.compile(expression, Pattern.CASE_INSENSITIVE); Matcher matcher = pattern.matcher(inputStr)

Java - Regex for Full Name

ぐ巨炮叔叔 提交于 2019-12-31 10:50:49
问题 How can I validate regex for full name? I only want alphabets (no numericals) and only spaces for the regex. This is what I have done so far. Would you please help me fix the regex? Thank you very much public static boolean isFullname(String str) { boolean isValid = false; String expression = "^[a-zA-Z][ ]*$"; //I know this one is wrong for sure >,< CharSequence inputStr = str; Pattern pattern = Pattern.compile(expression, Pattern.CASE_INSENSITIVE); Matcher matcher = pattern.matcher(inputStr)

Java - Regex for Full Name

一世执手 提交于 2019-12-31 10:47:33
问题 How can I validate regex for full name? I only want alphabets (no numericals) and only spaces for the regex. This is what I have done so far. Would you please help me fix the regex? Thank you very much public static boolean isFullname(String str) { boolean isValid = false; String expression = "^[a-zA-Z][ ]*$"; //I know this one is wrong for sure >,< CharSequence inputStr = str; Pattern pattern = Pattern.compile(expression, Pattern.CASE_INSENSITIVE); Matcher matcher = pattern.matcher(inputStr)