pattern-matching

pattern match args and give error messages in a lightweight Scala script

I write a number of simple scala scripts that end up starting with a simple pattern match on args like: val Array(path, foo, whatever) = args // .. rest of the script uses "path", "foo", etc. Of course, if I supply the wrong number of arguments, I get an inscrutable error like: scala.MatchError: [Ljava.lang.String;@7786df0f at Ma...

How do I manipulate parse trees?

I've been playing around with natural language parse trees and manipulating them in various ways. I've been using Stanford's Tregex and Tsurgeon tools but the code is a mess and doesn't fit in well with my mostly Python environment (those tools are Java and aren't ideal for tweaking). I'd like to have a toolset that would allow for easy ...

Best way to find multiple phrases in string?

Hi I want to process a String, in which I want to find multiple strings, i am trying to make some highlighter in html text in java.. Example: Find and process phrases table, row, primary key in Each table row contains a primary key column The text is html text with tags like <b>,<img..>... if there is ignorable tag in the middle of phras...

Pattern matching on the beginning of a string in f#

I am trying to match the beginning of strings in f#. Not sure if I have to treat them as a list of characters or what. Any suggestions would be appreciated. Here is a psuedo code version of what I am trying to do let text = "The brown fox.." match text with | "The"::_ -> true | "If"::_ -> true | _ -> false So, I want to look at th...

String chomping match expression with bound variable

Hi, The following shell sessions shows some behavior I would like to understand: 1> A = "Some text". "Some text" 2> "Some " ++ R = A. "Some text" 3> R. "text" 4> B = "Some ". "Some " 5> B ++ L = A. * 1: illegal pattern Surely statements 2 and 5 are syntacticially identical? I would like to use this idiom to extract some text from a s...

Unexpected Scala pattern matching syntax

I had a List of Scala tuples like the following: val l = List((1,2),(2,3),(3,4)) and I wanted to map it in a list of Int where each item is the sum of the Ints in a the corresponding tuple. I also didn't want to use to use the x._1 notation so I solved the problem with a pattern matching like this def addTuple(t: (Int, Int)) : Int = ...

bash filename start matching

Hello, I've got a simple enough question, but no guidance yet through the forums or bash. The question is as follows: I want to add a prefix string to each filename in a directory that matches *.h or *.cpp. HOWEVER, if the prefix has already been applied to the filename, do NOT apply it again. Why the following doesn't work is somethi...

Finding partial substrings within a string

I have two strings which must be compared for similarity. The algorithm must be designed to find the maximal similarity. In this instance, the ordering matters, but intervening (or missing) characters do not. Edit distance cannot be used in this case for various reasons. The situation is basically as follows: string 1: ABCDEFG string...

how do I pattern match a string within a string and then extract it into a variable

I have come across a problem that I cannot see to solve. I have extracted a line from a web page into a variable. lets say for argument sake this is: rhyme = "three blind mice Version 6.0" and I want to be able to first of all locate the version number within this string (6.0) and secondly extract this number into another seperate vari...

How can I extract the middle part of a string in FSharp?

I want to extract the middle part of a string using FSharp if it is quoted, similar like this: let middle = match original with | "\"" + mid + "\"" -> mid | all -> all But it doesn't work because of the infix operator + in pattern expression. How can I extract this? ...

Pattern matching against generic type using 'flexible' type parameter

match value with | :? list<#SomeType> as l -> l //Is it possible to match any list of a type derived from SomeType? | _ -> failwith "doesn't match" ...

Remove unallowed character from specific pattern in the php

I have a text field in which user can enter any character he/she wants. But in server i have a string patter [a-z0-9][a-z0-9+.-]*, if any of the character in the value from the text box doesn't match the pattern, then i must remove that character from that string. How can i do that in php. is there any functions for that? Thanks in adva...

How to detect links in a text ?

greetings all i have a text that may contain links something like: " hello..... visit us on http://www.google.com.eg, and for more info, please contact us on http://www.myweb.com/help" and i want to find and replace any link with the following link anyone knows how to do so ? and i have another question is that how any website li...

Template processing using Java

Dear All, We have an email template which needs to be processed using Java. We have to replace the variables in the template with actual values. We were able to achieve this using pattern matching , ie; by searching the template for particular patters and replace them with actual values. Now we need have conditions in the XML file.For ex...

Scala pattern matching confusion with Option[Any]

I have the following Scala code. import scala.actors.Actor object Alice extends Actor { this.start def act{ loop{ react { case "Hello" => sender ! "Hi" case i:Int => sender ! 0 } } } } object Test { def test = { (Alice !? (100, "Hello")) match { case i:Some[Int] => println ("Int rec...

Haskell - wildcard use on right side of guard in patterns

Lets say i have a piece of code like this: test pattern | pattern == (_,NOT (WIRE _)) = 1 | pattern == (_,AND (WIRE _) (WIRE _)) = 2 | otherwise = 0 Where i am trying to match it against one of several possibilities, some with one (WIRE ""), some with two. I have actual input as follows e.g.: ("p",NOT (WIRE "x")). I would like...

Java Pattern match

Hi All, I've a long template from which I need to extract certain strings based on certain patterns. When I went through some examples I found that use of quantifiers is good in such situations.For example following is my template, from which I need to extract while and doWhile. This is a sample document. $while($variable)This text can ...

Geometric Point Mapping using Rigid Motions

My problem is Given a set of fixed 2D points, N, and a set of corresponding 2D points, A, I want to find a mapping using a translation and a rotation which maps A onto N such that the largest distance between any two corresponding points is minimized. The points which correspond is known. I would also like to know the translation and/or...

regex - including strings: java

hi, I am trying to print out lines from a file which match a particular pattern in java. I am using the Pattern class for doing this. I tried putting the patter as "[harry]" so that every line which has "harry" gets printed out. But pattern always evaluates to false. My assumption is that the regex pattern I am entering is a string. M...

Help required to solve this regex issue

I need to get all characters between a particular expression. For example below is my sample document This is a sample document. $if ( $variable) FIRST This text can be repeated many times until do while is called. $elseif($variable2) Some sample text follows this. $endif I need to get all the characters be...