tag:blogger.com,1999:blog-5089773352404981635.post5337647694677275457..comments2023-10-04T08:15:13.812-07:00Comments on Daily scala: Regular Expression 3: Regex matchingAnonymoushttp://www.blogger.com/profile/07600430363435495915noreply@blogger.comBlogger9125tag:blogger.com,1999:blog-5089773352404981635.post-65434170681146350882011-10-07T06:46:32.014-07:002011-10-07T06:46:32.014-07:00I want to write st like that
m = [0-9]*
n = \.[0-9...I want to write st like that<br />m = [0-9]*<br />n = \.[0-9]*<br />o = mn?<br /><br />How can I do in Scala?Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-5089773352404981635.post-38951140494252458022011-09-28T08:41:47.130-07:002011-09-28T08:41:47.130-07:00As I said before there is nothing really scala rel...As I said before there is nothing really scala related to this question. If you google about regex in java you will see how you need to do escapes. <br /><br />However in this case you are trying to turn your string s into a regular expression. there is no matching going on. If you want to match: /** something */ you might do something like:<br /><br /><br />scala> val regex = """/\*.*\*/""".r<br />regex: scala.util.matching.Regex = /\*.*\*/<br /><br />scala> val Comment = """/\*\*(.*)\*/""".r<br />Comment: scala.util.matching.Regex = /\*\*(.*)\*/<br /><br />scala> val Comment(c) = "/** hello */"<br />c: String = " hello "<br /><br />Comment is a regex based extractor. I used """ so I don't need to use double \\ for escaping each *. the quotes are around .* because that matches anything. So it will capture every thing between a /** and */.Anonymoushttps://www.blogger.com/profile/07600430363435495915noreply@blogger.comtag:blogger.com,1999:blog-5089773352404981635.post-38750896496951265572011-09-28T08:25:27.177-07:002011-09-28T08:25:27.177-07:00i tried to what Tong asked...
here is my code: (&...i tried to what Tong asked...<br /><br />here is my code: ("/\\*.\\*/").r<br /><br />val s = "/**something*/"<br />s.r<br /><br />and my error is Dangling meta character "*" near....<br /><br />and I fix my String like that: <br />val s = "/*\\*something*/"<br />s.r<br /><br />it works, but i don't know how it work. Can't you explain me, Jesse?Rec4rxnoreply@blogger.comtag:blogger.com,1999:blog-5089773352404981635.post-83297283487920708852011-09-26T22:57:19.019-07:002011-09-26T22:57:19.019-07:00@Eric. If you do somthing like:
val RegEx = &quo...@Eric. If you do somthing like:<br /><br />val RegEx = "(h.*?)".r<br /> <br />val RegEx(hParam) = "what's up"<br /><br />the previous line will throw an exception because "what's up" doesn't start with an 'h'. If you are not absolutely sure you should do:<br /><br />val hParam = "what's up" match {<br /> case RegEx(h) => h<br /> case _ => "doesn't match h"<br />}<br /><br />to handle the case where the regex doesn't apply.Anonymoushttps://www.blogger.com/profile/07600430363435495915noreply@blogger.comtag:blogger.com,1999:blog-5089773352404981635.post-12771599549431355702011-09-26T22:54:12.748-07:002011-09-26T22:54:12.748-07:00@Tong: It is standard Regex stuff nother special...@Tong: It is standard Regex stuff nother special with Scala for escaping * and other regex characters. The standard escape is \ but you can use quoting as well like:<br /><br />\Qthis is a special quoted regex section *\E<br /><br />Of course it is better to use triple quoted regexes so you don't have to escape your escapes like in JavaAnonymoushttps://www.blogger.com/profile/07600430363435495915noreply@blogger.comtag:blogger.com,1999:blog-5089773352404981635.post-51451510887891845472011-09-26T18:39:19.550-07:002011-09-26T18:39:19.550-07:00i want to find if it a comments /*....*/ how can i...i want to find if it a comments /*....*/ how can i find match * chractor?adminhttps://www.blogger.com/profile/05736272014388786339noreply@blogger.comtag:blogger.com,1999:blog-5089773352404981635.post-68711152197691066222010-07-22T10:26:42.948-07:002010-07-22T10:26:42.948-07:00What exactly do you mean by:
/*
If you want to us...What exactly do you mean by: <br />/*<br />If you want to use Regex's in assignment you must be sure the match will work. Otherwise you should do real matching<br />*/<br /><br />I am trying to use this type of assignment matching with the RegEx object thinking that a non-match will just not match and continue, but Scala exits and from what I've read, you're not supposed to catch MatchErrors -- does this mean assignment with the RegEx object should only EVER be used if you're certain there will be a match?Erik Hansenhttp://www.erikdhansen.comnoreply@blogger.comtag:blogger.com,1999:blog-5089773352404981635.post-54762218572117365162010-01-13T00:25:00.375-08:002010-01-13T00:25:00.375-08:00I am using Scala 2.8. I will update the postI am using Scala 2.8. I will update the postAnonymoushttps://www.blogger.com/profile/07600430363435495915noreply@blogger.comtag:blogger.com,1999:blog-5089773352404981635.post-15588461014848079982010-01-12T17:56:26.060-08:002010-01-12T17:56:26.060-08:00What version of Scala are you using? The last set...What version of Scala are you using? The last set of examples doesn't work for me in the 2.7.5 REPL. I get the following for the very last example(assuming you meant "d : _*" instead of "d @ _*):<br /><br />error: ')' expected but identifier found.<br /><br />And for the two examples prior to that, I get this error:<br /><br /> error: recursive value x$1 needs type<br /><br />So I wrote it as <br /><br />val Seq(one:String, two:String) = ...<br /><br />But then I got this:<br /><br /> error: value toSeq is not a member of scala.util.matching.Regex.MatchIterator<br /><br />So my first thought was that I might need 2.8 to make this work. Is that true?Anonymousnoreply@blogger.com