pattern-matching

Pattern matching a variable in OCaml?

假装没事ソ 提交于 2020-01-03 18:33:07
问题 If I do let check n = function | n -> true | _ -> false then I get Warning 11: this match case is unused. I understand why , since the n in | n -> true is actually not the argument of check . It is basically a variable created by the pattern matching. My question is , in this case, do we have any way to still using pattern matching (instead of if else) to force this check? I.e., I want to pattern match with the argument n . 回答1: You can use when to have patterns along with boolean conditions:

How do I check if a string matches a set pattern in Python?

随声附和 提交于 2020-01-03 17:01:32
问题 I want to match a string to a specific pattern or set of words, like below: the apple is red is the query and the apple|orange|grape is red|orange|violet is the pattern to match. The pipes would represent words that would substitute each other. The pattern could also be grouped like [launch app]|[start program] . I would like the module to return True or False whether the query matches the pattern, naturally. What is the best way to accomplish this if there is not a library that does this

Splitting string to parts with specific pattern and conditions

百般思念 提交于 2020-01-03 16:58:47
问题 I have the below-like array of about 5k+ strings as output from certain application (for security reasons I may not provide the exact data, but the example format is pretty much similar to the actual data): kasdfhkasdhfaskdfj42345sdsadkfdkfhasdf5345534askfhsad asdfasdf66sdafsdfsdf4560sdfasdfasdf sdfaasdfs96sadfasdf65459asdfasdf sadfasdf8asdfasdas06666654asdfasdfsd fasdjfsdjfhgasdf6456sadfasdfasdf9sdfasdfsadf Simply, I have non-breaking alphanumeric string that consists of 5 parts : [latin

Splitting string to parts with specific pattern and conditions

假装没事ソ 提交于 2020-01-03 16:58:22
问题 I have the below-like array of about 5k+ strings as output from certain application (for security reasons I may not provide the exact data, but the example format is pretty much similar to the actual data): kasdfhkasdhfaskdfj42345sdsadkfdkfhasdf5345534askfhsad asdfasdf66sdafsdfsdf4560sdfasdfasdf sdfaasdfs96sadfasdf65459asdfasdf sadfasdf8asdfasdas06666654asdfasdfsd fasdjfsdjfhgasdf6456sadfasdfasdf9sdfasdfsadf Simply, I have non-breaking alphanumeric string that consists of 5 parts : [latin

Threading `Try`s through for-comprehension

十年热恋 提交于 2020-01-03 16:45:54
问题 Triggered by another question (which has been subsequently edited away though), I wanted to try out how easy it would be to chain calls to Scala 2.10's Try construct (cf. this presentation), using for-comprehensions. The idea is to have a list of tokens and match them against a sequence of patterns, then return the first error or the successfully matched pattern. I arrived at the following pretty awkward version, and I wonder if this can be made simpler and nicer: import util.Try trait Token

Threading `Try`s through for-comprehension

两盒软妹~` 提交于 2020-01-03 16:44:16
问题 Triggered by another question (which has been subsequently edited away though), I wanted to try out how easy it would be to chain calls to Scala 2.10's Try construct (cf. this presentation), using for-comprehensions. The idea is to have a list of tokens and match them against a sequence of patterns, then return the first error or the successfully matched pattern. I arrived at the following pretty awkward version, and I wonder if this can be made simpler and nicer: import util.Try trait Token

Filter log by matching pattern - log4j

ぐ巨炮叔叔 提交于 2020-01-03 12:32:39
问题 I have the following layout patter in my log4j xml file: "%d{ISO8601} %c %p [%t] [%x] 9.5.4.RC12 %m%n" What I want is that when ever I get a log containing message process [proc#] completed , it should be skipped. I mean every log except the one containing this message should be printed. [proc#] will contain the process number of max lenght 4. What can I desing a filter with this function in my xml config file. If so, then how? 回答1: ExpressionFilter can do that. In a filter definition inside

Filter log by matching pattern - log4j

£可爱£侵袭症+ 提交于 2020-01-03 12:32:27
问题 I have the following layout patter in my log4j xml file: "%d{ISO8601} %c %p [%t] [%x] 9.5.4.RC12 %m%n" What I want is that when ever I get a log containing message process [proc#] completed , it should be skipped. I mean every log except the one containing this message should be printed. [proc#] will contain the process number of max lenght 4. What can I desing a filter with this function in my xml config file. If so, then how? 回答1: ExpressionFilter can do that. In a filter definition inside

How to write to a file using awk to find a string in between ( ); and add a printline after ; to print the string found?

[亡魂溺海] 提交于 2020-01-02 15:03:06
问题 I have some files in a folder that has several sqrt(x+y); functions. I want to find out what is in x+y. So I want to read the value inside sqrt(x+y ); and add a printf(x+y ); after the semicolon. I was trying this for about a month. But still cannot find a way. Please help. If you know where I can find any good tutorial or book about AWK please let me know.. Thanks. Following is the algorithm that I figured. (I am totally new to AWK) loop thro all files{ if one line starts with sqrt( start

android.util.Patterns.EMAIL_ADDRESS is validating invalid emails

放肆的年华 提交于 2020-01-02 07:21:10
问题 These are few emails which are not valid. email@domain.web .email@domain.com I have checked above emails in following websites, All of those returns invalid. http://isemail.info/about http://sqa.fyicenter.com/Online_Test_Tools/Email_Address_Format_Validator.php. Yet, android.util.Patterns.EMAIL_ADDRESS pattern validates both. Is there a bug or am I missing something? 回答1: Both seems to be valid email addresses email@domain.web .email@domain.com since any email address contains three