matcher

Java recursive(?) repeated(?) deep(?) pattern matching

这一生的挚爱 提交于 2019-12-01 17:43:35
问题 I'm trying to get ALL the substrings in the input string that match the given pattern. For example, Given string: aaxxbbaxb Pattern: a[a-z]{0,3}b (What I actually want to express is: all the patterns that starts with a and ends with b, but can have up to 2 alphabets in between them) Exact results that I want (with their indexes): aaxxb: index 0~4 axxb: index 1~4 axxbb: index 1~5 axb: index 6~8 But when I run it through the Pattern and Matcher classes using Pattern.compile() and Matcher.find()

RSpec: Expect to change multiple

霸气de小男生 提交于 2019-11-30 10:26:31
问题 I want to check for many changes in a model when submitting a form in a feature spec. For example, I want to make sure that the user name was changed from X to Y, and that the encrypted password was changed by any value. I know there are some questions about that already, but I didn't find a fitting answer for me. The most accurate answer seems like the ChangeMultiple matcher by Michael Johnston here: Is it possible for RSpec to expect change in two tables?. Its downside is that one only

RSpec: Expect to change multiple

不问归期 提交于 2019-11-29 20:23:17
I want to check for many changes in a model when submitting a form in a feature spec. For example, I want to make sure that the user name was changed from X to Y, and that the encrypted password was changed by any value. I know there are some questions about that already, but I didn't find a fitting answer for me. The most accurate answer seems like the ChangeMultiple matcher by Michael Johnston here: Is it possible for RSpec to expect change in two tables? . Its downside is that one only check for explicit changes from known values to known values. I created some pseudo code on how I think a

Mockito match any class argument

回眸只為那壹抹淺笑 提交于 2019-11-29 19:38:29
Is there a way to match any class argument of the below sample routine? class A { public B method(Class<? extends A> a) {} } How can I always return a new B() regardless of which class is passed into method ? The following attempt only works for the specific case where A is matched. A a = new A(); B b = new B(); when(a.method(eq(A.class))).thenReturn(b); EDIT : One solution is (Class<?>) any(Class.class) Two more ways to do it (see my comment on the previous answer by @Tomasz Nurkiewicz): The first relies on the fact that the compiler simply won't let you pass in something of the wrong type:

All overlapping substrings matching a java regex

巧了我就是萌 提交于 2019-11-29 11:02:29
Is there an API method that returns all (possibly overlapping) substrings that match a regular expression? For example, I have a text string: String t = 04/31 412-555-1235; , and I have a pattern: Pattern p = new Pattern("\\d\\d+"); that matches strings of two or more characters. The matches I get are: 04, 31, 412, 555, 1235. How do I get overlapping matches? I want the code to return: 04, 31, 41, 412, 12, 55, 555, 55, 12, 123, 1235, 23, 235, 35. Theoretically it should be possible -- there is an obvious O(n^2) algorithm that enumerates and checks all the substrings against the pattern. EDIT

Regular expression to extract SQL query

筅森魡賤 提交于 2019-11-29 08:21:45
Is there a regex which extracts SQL queries from a string? I'm NOT interested to validate any SQL syntax, rather and only extracting a selection of SQL commands. This to parse a given SQL file/string in a flexible manner. Given is the following SQL file/string example: SELECT * FROM test_table WHERE test_row = 'Testing ; semicolon'; SELECT * FROM another_test_table; INSERT INTO table_name VALUES (value1,'value which contains semicolon ;;;;',value3,...); Some pseudocode example would be: ^(UPDATE|SELECT|INSERT INTO)(.*)(;)$ . In the future i'm looking to extend this with all (possible) commands

Jasmine toEqual for complex objects (mixed with functions)

泄露秘密 提交于 2019-11-29 03:13:05
Currently, I have a function that sometimes return an object with some functions inside. When using expect(...).toEqual({...}) it doesn't seem to match those complex objects. Objects having functions or the File class (from input type file), it just can't. How to overcome this? Try the Underscore _.isEqual() function: expect(_.isEqual(obj1, obj2)).toEqual(true); If that works, you could create a custom matcher : this.addMatchers({ toDeepEqual: function(expected) { return _.isEqual(this.actual, expected); }); }); So you can then write specs like so: expect(some_obj).toDeepEqual(expected_obj);

get unique regex matcher results (without using maps or lists)

天涯浪子 提交于 2019-11-29 02:12:01
Is there a way to get only the unique matches? without using a list or a map after the matching, I want the matcher output to be unique right away. Sample input/output: String input = "This is a question from [userName] about finding unique regex matches for [inputString] without using any lists or maps. -[userName]."; Pattern pattern = Pattern.compile("\\[[^\\[\\]]*\\]"); Matcher matcher = pattern.matcher(rawText); while (matcher.find()) { String tokenName = matcher.group(0); System.out.println(tokenName); } This will output the following: [userName] [inputString] [userName] But I want it to

get unique regex matcher results (without using maps or lists)

有些话、适合烂在心里 提交于 2019-11-29 02:10:11
Is there a way to get only the unique matches? without using a list or a map after the matching, I want the matcher output to be unique right away. Sample input/output: String input = "This is a question from [userName] about finding unique regex matches for [inputString] without using any lists or maps. -[userName]."; Pattern pattern = Pattern.compile("\\[[^\\[\\]]*\\]"); Matcher matcher = pattern.matcher(rawText); while (matcher.find()) { String tokenName = matcher.group(0); System.out.println(tokenName); } This will output the following: [userName] [inputString] [userName] But I want it to

Convert Javascript regular expression to Java syntax

南笙酒味 提交于 2019-11-28 18:21:48
I am aware that regEx are common across languages...But I am having trouble in writing the Java syntax. I have a regular expression coded in JS as; if((/[a-zA-Z]/).test(str) && (/[0-9]|[\x21-\x2F|\x3A-\x40|\x5B-\x60|\x7B-\x7E]/).test(str)) return true; How do I write the same in Java ? I have imported import java.util.regex.Matcher; import java.util.regex.Pattern; Just to add, from what I am trying it is saying \x is an invalid escape character.. Change the leading and trailing '/' characters to '"' , and then replace each '\' with "\\" . Unlike, Javascript, Perl and other scripting languages,