Scala substring match to match noun [duplicate] - regex

This question already has answers here:
How to compare a string with another where the one has space in between
(4 answers)
Closed 4 years ago.
Is there any regex to match substring if they have space in between in scala ?
For eg:
"hero 6 go pro" contains "gopro" should return true
"go pro hero 6 " contains "gopro" should return true
I tried :
def matchWords(input: Seq[Char], words: Seq[Char]): Boolean = (input, words) match {
case (Seq(), Seq() | Seq(' ', _*)) => true
case (Seq(), _) => false
case (Seq(a, tail#_*), Seq(b, rest#_*)) if a == b => matchWords(tail, rest)
case (_, Seq(' ', rest#_*)) => matchWords(input, rest)
case _ => false
}
but
matchWords("gopro", "hero 6 go pro") returns false
though this matchWords("fitbit", "fit bit versa") return true.
The string should match nouns.
Any idea what I am doing wrong here ?
Thanks,
Shalini

A user with the same "name" as you has already asked a very similar question here and been given multiple answers, including one from me.
The code in your question appears to be copied from another of those answers. Unfortunately you picked the lowest-scoring answer which doesn't give the right results.
I suggest that you try my answer from that question...

Related

How can I pass this case rule in Scala to only accept string containing letters?

I'm working on learning Scala through the Exercism exercises and am stuck on the Bob exercise. My goal in the second to last case is to answer Whoa, chill out! if a given statement has capital letters but can also contain anything else aside from a question mark. I can't seem to find a way to achieve this without contradicting another rule. I'm not sure if isLetter is an actual function but my Exercism mentor suggested I use it.
object Bob {
def response(statement: String): String = {
// println(statement)
println("k".contains("abcdefghijklmnopqrstuvwxyz".toCharArray))
statement.trim() match {
case x if x.isEmpty => "Fine. Be that way!"
case x if x.matches("^[A-Z0-9][^a-z].+\\?$") => "Calm down, I know what I'm doing!"
case x if x.matches("^.+\\?$") => "Sure."
case x if (x.matches ("[A-Z %\\^*##$(*\\!,)0-9]+") /*&& x.isLetter*/) => "Whoa, chill out!"
case _ => "Whatever."
}
}
}
The test cases I'm running into trouble with:
"1, 2, 3 GO!"
"1, 2, 3"
1, 2, 3 GO! should be who, chill out.
Whereas 1, 2, 3 should be whatever but always returns as whoa chill out.
Thanks.
From the limited explanation provided, I think that the "Whoa..." case is supposed to check for two conditions,
Presense of at least 1 captital letter.
Question mark can not be present at any place at all.
If these are your requirements then your "Whoa..." case can be simply written as an AND of these two conditions.
case x if (x.matches("(.*)([A-Z]+)(.*)") && !x.matches("(.*)(\\?)(.*)")) => "Whoa, chill out!"
You can use a single pattern with a positive lookahead asserting a char A-Z, and then matching any char except a question mark.
^(?=[^A-Z]*[A-Z\n])[^?\n]+$
Explanation
^ Start of string
(?= Positive lookahead, assert that on the right is
[^A-Z\n]*[A-Z] Match any char other than a newline or A-Z, then match A-Z
) Close lookahead
[^?\n]+ Match 1+ chars other than ? or a newline
$ End of string
Regex demo | Scala demo
For example
object Bob {
def response(statement: String): String = {
statement.trim() match {
case x if x.isEmpty => "Fine. Be that way!"
case x if x.matches("^[A-Z0-9\\n][^a-z].+\\?$") => "Calm down, I know what I'm doing!"
case x if x.matches("^.+\\?$") => "Sure."
case x if x.matches ("^(?=[^A-Z]*[A-Z])[^?\\n]+$") => "Whoa, chill out!"
case _ => "Whatever."
}
}
}
Bob.response("1, 2, 3 GO!")
Bob.response("1, 2, 3")
Output
val res0: String = Whoa, chill out!
val res1: String = Whatever.

Issue with REGEX in javascript [duplicate]

This question already has answers here:
How to tell if a string contains a certain character in JavaScript?
(21 answers)
Closed 4 years ago.
I get an issue using REGEX, it is probably about my REGEX but I need some helps.
I need to match all string containing "D"...
Test string 1 : D
Test string 2 : aaaaaaDqqqqq
Test string 3 : Dssssssss
Test string 4 : D4564646
Test string 5 : 1321313D2312
Test string 6 : ppppprrrrrr
My regex :
/^.+D.+|(:?^|\s)D$/gi
It works only for 1 and 2 and it should works for 1, 2, 3, 4 and 5.
In your case problem is with + operator which is literally Matches between one and unlimited times so it wont work if letter "D" will be in the beggining or the end of string. Try this regex: ^.*D.*$ with asterix, as it is defined as Matches between zero and unlimited times
See example
Following regex should work for you
.*D.*
If all you need to do is test for whether or not a string contains a D character, it's just /D/
var tests = [
"D",
"aaaaaaDqqqqq",
"Dssssssss",
"D4564646",
"1321313D2312",
"ppppprrrrrr"
]
tests.forEach(str => console.log(/D/.test(str)))
Instead of using a regex simply use the includes function
var string = "aaaaaaDqqqqq",
substring = "D";
if(string.includes(substring)){
console.log("contain")
}else{
console.log("don't contain")
}

Scala regex find matches in middle of string [duplicate]

This question already has an answer here:
Working regex fails when using Scala pattern matching
(1 answer)
Closed 5 years ago.
I have written the following code in scala:
val regex_str = "([a-z]+)(\\d+)".r
"_abc123" match {
case regex_str(a, n) => "found"
case _ => "other"
}
which returns "other", but if I take off the leading underscore:
val regex_str = "([a-z]+)(\\d+)".r
"abc123" match {
case regex_str(a, n) => "found"
case _ => "other"
}
I get "found". How can I find any ([a-z]+)(\\d+) instead of just at the beginning? I am used to other regex languages where you use a ^ to specify beginning of the string, and the absence of that just gets all matches.
Scala regex patterns default as "anchored", i.e. bound to beginning and end of target string.
You'll get the expected match with this.
val regex_str = "([a-z]+)(\\d+)".r.unanchored
Hi May be you need something like this,
val regex_str = "[^>]([a-z]+)(\\d+)".r
"_abc123" match {
case regex_str(a, n) => println(s"found $a $n")
case _ => println("other")
}
This will avoid the first character from your string.
Hope this helps!
The unapplySeq of the Regex tries to capture the whole input by default (treats the pattern as if it was between ^ and $).
There are two ways to capture inside the input:
use .* before and after the captures: val regex_str = ".*([a-z]+)(\\d+).*".r
do the same with .unanchored: val regex_str = "([a-z]+)(\\d+)".r.unanchored
Otherwise scala treats regular expression anchors the same way as in other languages; this one is an exception made for semantic reasons.
The regex extractor in scala pattern-matching attempts to match the entire string. If you want to skip some junk-characters in the beginning and in the end, prepend a . with a reluctant quantifier to the regex:
val regex_str = ".*?([a-z]+)(\\d+).*".r
val result = "_!+<>__abc123_%$" match {
case regex_str(a, n) => s"found a = '$a', n = '$n'"
case _ => "no match"
}
println(result)
This outputs:
found a = 'abc', n = '123'
Otherwise, don't use the pattern match with the extractor, use "...".r.findAllIn to find all matches.

Case matching with R's gsub? [duplicate]

This question already has answers here:
How do I replace the string exactly using gsub()
(1 answer)
Matching entire string in R
(3 answers)
Closed 9 years ago.
I am trying to write a code in R to change all "non" word to "none" in a string but I don't want to change any other words, specially I don't want to change "none" to "nonee".
I tried this code:
gsub("non","none", "non none", fixed = TRUE)
but result is:
"none nonee"
Is there anyway to do this using R's gsub?
You can use word boundaries...
x <- c("non" , "none")
gsub( "\\bnon\\b" , "none" , x )
#[1] "none" "none"

Scala regex "starts with lowercase alphabets" not working

val AlphabetPattern = "^([a-z]+)".r
def stringMatch(s: String) = s match {
case AlphabetPattern() => println("found")
case _ => println("not found")
}
If I try,
stringMatch("hello")
I get "not found", but I expected to get "found".
My understanding of the regex,
[a-z] = in the range of 'a' to 'z'
+ = one more of the previous pattern
^ = starts with
So regex AlphabetPattern is "all strings that start with one or more alphabets in the range a-z"
Surely I am missing something, want to know what.
Replace case AlphabetPattern() with case AlphabetPattern(_) and it works. The extractor pattern takes a variable to which it binds the result. Here we discard it but you could use x or whatever.
edit: Further to Randall's comment below, if you check the docs for Regex you'll see that it has an unapplySeq rather than an unapply method, which means it takes multiple variables. If you have the wrong number, it won't match, rather like
list match { case List(a,b,c) => a + b + c }
won't match if list doesn't have exactly 3 elements.
There are some issues with the match statement. s match is matching on the value of s which is checked against AlphabetPattern and _ which always evaluates to _ since s is never equal to "^([a-z]+)".r. Use one of the find methods in Scala.Util.Regex to look for a match with the given `Regex.
For example, using findFirstIn to find the first match of a string in AlphabetPattern.
scala> AlphabetPattern.findFirstIn("hello")
res0: Option[String] = Some(hello)
The stringMatch method using findFirstIn and a case statement:
scala> def stringMatch(s: String) = AlphabetPattern findFirstIn s match {
| case Some(s) => println("Found: " + s)
| case None => println("Not found")
| }
stringMatch: (s:String)Unit
scala> stringMatch("hello")
Found: hello