pattern-matching

Regular Expression backtracks until overflow in Java

筅森魡賤 提交于 2019-12-31 05:37:07
问题 The following expression: ^(#ifdef FEATURE)+?\s*$((\r\n.*?)*^(#endif)+\s*[\/\/]*\s*(end of)*\s*FEATURE)+?$ Overrides the matching buffer when running my compiled .Jar file. The matching string can be similar to: this is a junk line #ifdef FEATURE #endif // end of FEATURE this is a junk line #ifdef FEATURE this is a junk line that should be matched: HOLasduiqwhei & // FEATURE fjfefj #endif // h #endif FEATURE this is a junk line So, the bold strings should match. The error is as follows: at

Python regex: tokenizing English contractions

谁都会走 提交于 2019-12-31 01:59:06
问题 I am trying to parse strings in such a way as to separate out all word components, even those that have been contracted. For example the tokenization of "shouldn't" would be ["should", "n't"]. The nltk module does not seem to be up to the task however as: "I wouldn't've done that." tokenizes as: ['I', "wouldn't", "'ve", 'done', 'that', '.'] where the desired tokenization of "wouldn't've" was: ['would', "n't", "'ve"] After examining common English contractions, I am trying to write a regex to

Why can't upper-case letters be used for pattern matching for define values?

荒凉一梦 提交于 2019-12-30 19:48:11
问题 Why can I use lower-case letters for names: val (a, bC) = (1, 2) (1, 2) match { case (a, bC) => ??? } and can't use upper-case letters: /* compile errors: not found: value A, BC */ val (A, BC) = (1, 2) /* compile errors: not found: value A, BC */ (1, 2) match { case (A, BC) => ??? } I'm using scala-2.11.17 回答1: Because the designers of Scala preferred to allow identifiers starting with upper-case letters to be used like this (and allowing both would be confusing): val A = 1 2 match { case A =

Multiple assignment via pattern matching with Array is not working with uppercase vals

為{幸葍}努か 提交于 2019-12-30 18:33:10
问题 After reading this answer I've tried to play with this nice feature by myself and found out that it is ok when I'm do scala> val Array(a,b,n) = "XXX,YYY,ZZZ".split(",") a: java.lang.String = XXX b: java.lang.String = YYY n: java.lang.String = ZZZ But is not ok with uppercase named variable: scala> val Array(a,b,N) = "XXX,YYY,ZZZ".split(",") <console>:9: error: not found: value N val Array(a,b,N) = "XXX,YYY,ZZZ".split(",") What is the reason of such behavior? UPD Actually, the same thing with

Java Pattern/ Matcher

耗尽温柔 提交于 2019-12-30 10:26:24
问题 This is a sample text: \1f\1e\1d\020028 . I cannot modify the input text, I am reading long string of texts from a file. I want to extract the following: \1f , \1e , \1d , \02 For this, I have written the following regular expression pattern: "\\[a-fA-F0-9]" I am using Pattern and Matcher classes, but my matcher is not able find the pattern using the mentioned regular expression. I have tested this regex with the text on some online regex websites and surprisingly it works there. Where am I

Escaping special characters in Perl regex

﹥>﹥吖頭↗ 提交于 2019-12-30 08:52:49
问题 I'm trying to match a regular expression in Perl. My code looks like the following: my $source = "Hello_[version]; Goodbye_[version]"; my $pattern = "Hello_[version]"; if ($source =~ m/$pattern/) { print "Match found!" } The problem arises in that brackets indicate a character class (or so I read) when Perl tries to match the regex, and the match ends up failing. I know that I can escape the brackets with \[ or \] , but that would require another block of code to go through the string and

Escaping special characters in Perl regex

故事扮演 提交于 2019-12-30 08:51:08
问题 I'm trying to match a regular expression in Perl. My code looks like the following: my $source = "Hello_[version]; Goodbye_[version]"; my $pattern = "Hello_[version]"; if ($source =~ m/$pattern/) { print "Match found!" } The problem arises in that brackets indicate a character class (or so I read) when Perl tries to match the regex, and the match ends up failing. I know that I can escape the brackets with \[ or \] , but that would require another block of code to go through the string and

Haskell Pattern Matching on the Empty Set

╄→尐↘猪︶ㄣ 提交于 2019-12-30 07:54:15
问题 I'm changing some Haskell code from using lists to sets. I understand everything required, I think, but I'm not sure how to pattern match on sets. Lists have this nice literal syntax that seems hard to emulate with the Set constructor. For example, I might have some code like this: foo [] = [] foo x = other_thing How can I write this code so it uses Sets instead of lists? 回答1: Well, you can't. Set is an abstract data type [0] that deliberately hides its internal representation, primarily to

Haskell (n+1) in pattern matching

若如初见. 提交于 2019-12-30 07:53:10
问题 I was doing the 99 Problems in Haskell when I encountered a solution to Problem 19 that I did not fully understand. The task is to write a rotate function that works like this *Main> rotate ['a','b','c','d','e','f','g','h'] 3 "defghabc" *Main> rotate ['a','b','c','d','e','f','g','h'] (-2) "ghabcdef" One provided solution is rotate [] _ = [] rotate l 0 = l rotate (x:xs) (n+1) = rotate (xs ++ [x]) n rotate l n = rotate l (length l + n) I don't understand how the pattern matching can ever reach

Matching with Opening/Closing Pair Characters in Regular Expressions

懵懂的女人 提交于 2019-12-30 07:24:27
问题 First, a little background. I have strings that resemble the following: ((Foo.Bar.StartsWith("A")) && (Foo.Bar.EndsWith("B"))) And I'm trying to reformat these to look like this: ((Foo.Any(Bar.StartsWith("A"))) && (Foo.Any(Bar.EndsWith("B")))) Side Note: The parts following the .Bar may sometimes not include (), like .Bar == "AB" . I'm trying to capture two groups: the Foo. and the Bar.StartsWith("<A or B>") . I worked out the following pattern but it drops the trailing parenthesis. \((Foo\.)