pattern-matching

Javascript Regex: replacing the last dot for a comma

不打扰是莪最后的温柔 提交于 2019-12-30 06:44:26
问题 I have the following code: var x = "100.007" x = String(parseFloat(x).toFixed(2)); return x => 100.01 This works awesomely just how I want it to work. I just want a tiny addition, which is something like: var x = "100,007" x.replace(",", ".") x.replace x = String(parseFloat(x).toFixed(2)); x.replace(".", ",") return x => 100,01 However, this code will replace the first occurrence of the ",", where I want to catch the last one. Any help would be appreciated. 回答1: You can do it with a regular

Implying equality in a Haskell pattern match

流过昼夜 提交于 2019-12-30 06:10:50
问题 I'm writing a function to simplify a Boolean expression. For example, Nand(A, A) == Not(A) . I've tried to implement this particular rule using pattern matching, like so: -- Operands equivalent - simplify! simplify (Nand q q) = Not (simplify q) -- Operands must be different, so recurse. simplify (Nand q q') = Nand (simplify q) (simplify q') Upon compiling, I get the error: Conflicting definitions for `q' Bound at: boolean.hs:73:21 boolean:73:29 In an equation for `simplify' I think I

What patterns does PowerShell -like operator support?

戏子无情 提交于 2019-12-30 06:06:37
问题 I was not able to find any reference about syntax of the PowerShell -like operator. Windows PowerShell reference on comparison operators states only: -Like Description: Match using the wildcard character (*). Though by experimenting I've found that it also supports the ? and the [] (set). Is there any other syntax supported? Is there any definitive reference to the -like operator? 回答1: The help for about_Comparison_Operators indeed claims wildcard support. It fails to specify what, exactly, a

How do we match a suffix in a string in bash?

对着背影说爱祢 提交于 2019-12-30 06:03:31
问题 I want to check if an input parameter ends with ".c"? How do I check that? Here is what I got so far (Thanks for your help): #!/bin/bash for i in $@ do if [$i ends with ".c"] then echo "YES" fi done 回答1: A classical case for case ! case $i in *.c) echo Yes;; esac Yes, the syntax is arcane, but you get used to it quickly. Unlike various Bash and POSIX extensions, this is portable all the way back to the original Bourne shell. 回答2: $ [[ foo.c = *.c ]] ; echo $? 0 $ [[ foo.h = *.c ]] ; echo $? 1

Haskell Bytestrings: How to pattern match?

怎甘沉沦 提交于 2019-12-30 00:33:06
问题 I'm a Haskell newbie, and having a bit of trouble figuring out how to pattern match a ByteString . The [Char] version of my function looks like: dropAB :: String -> String dropAB [] = [] dropAB (x:[]) = x:[] dropAB (x:y:xs) = if x=='a' && y=='b' then dropAB xs else x:(dropAB $ y:xs) As expected, this filters out all occurrences of "ab" from a string. However, I have problems trying to apply this to a ByteString . The naive version dropR :: BS.ByteString -> BS.ByteString dropR [] = [] dropR (x

Haskell Bytestrings: How to pattern match?

荒凉一梦 提交于 2019-12-30 00:33:03
问题 I'm a Haskell newbie, and having a bit of trouble figuring out how to pattern match a ByteString . The [Char] version of my function looks like: dropAB :: String -> String dropAB [] = [] dropAB (x:[]) = x:[] dropAB (x:y:xs) = if x=='a' && y=='b' then dropAB xs else x:(dropAB $ y:xs) As expected, this filters out all occurrences of "ab" from a string. However, I have problems trying to apply this to a ByteString . The naive version dropR :: BS.ByteString -> BS.ByteString dropR [] = [] dropR (x

Regexp to validate URL in MySQL

雨燕双飞 提交于 2019-12-29 09:02:45
问题 I have tried several regex patterns (designed for use with PHP because I couldn't find any for MySQL) for URL validation, but none of them are working. Probably MySQL has a slightly different syntax. I've also tried to come up with one, but no success. So does anyone know a fairly good regex to use with MySQL for URL validation? 回答1: According to article 11.5.2. Regular Expressions in MySQL's documentation, you can perform selections with a regular expression with the following syntax SELECT

Python, how to implement something like .gitignore behavior

末鹿安然 提交于 2019-12-29 06:46:49
问题 I need to list all files in the current directory (.) (including all sub directories), and exclude some files as how .gitignore works (http://git-scm.com/docs/gitignore) With fnmatch (https://docs.python.org/2/library/fnmatch.html) I will be able to "filter" files using a pattern ignore_files = ['*.jpg', 'foo/', 'bar/hello*'] matches = [] for root, dirnames, filenames in os.walk('.'): for filename in fnmatch.filter(filenames, '*'): matches.append(os.path.join(root, filename)) how can I

How does language detection work?

泄露秘密 提交于 2019-12-29 04:42:31
问题 I have been wondering for some time how does Google translate(or maybe a hypothetical translator) detect language from the string entered in the "from" field. I have been thinking about this and only thing I can think of is looking for words that are unique to a language in the input string. The other way could be to check sentence formation or other semantics in addition to keywords. But this seems to be a very difficult task considering different languages and their semantics. I did some

Scala: strange behavior in `for` pattern matching for None case

久未见 提交于 2019-12-29 01:37:08
问题 Strange behavior in for cycle pattern matching: scala> val a = Seq(Some(1), None) a: Seq[Option[Int]] = List(Some(1), None) scala> for (Some(x) <- a) { println(x) } 1 scala> for (None <- a) { println("none") } none none Why in second example two output 'none' produced? Maybe this example is synthetic and not practical, but such behavior is not expectable. Is this bug or feature? 回答1: What do you know, it is a bug: https://issues.scala-lang.org/browse/SI-9324 scala> val vs = Seq(Some(1), None)