How to check if a word is present in a sentence using Java? [duplicate]
try regex boolean contains = s.matches(“.*\\bram\\b.*”); \b means word boundary
try regex boolean contains = s.matches(“.*\\bram\\b.*”); \b means word boundary
You can use a view type and view patterns to do what you want: module ThingModule (Thing, ThingView(..), view) where data Thing = Foo Thing | Bar Int data ThingView = FooV Thing | BarV Int view :: Thing -> ThingView view (Foo x) = FooV x view (Bar y) = BarV y Note that … Read more
My first thought is to try to parse the names into a description of features (company LG, size 42 Inch, resolution 1080p, type LCD HDTV). Then you can match these descriptions against each other for compatibility; it’s okay to omit a product number but bad to have different sizes. Simple are-the-common-attributes-compatible might be enough, or … Read more
Yes, the whole point of pre-compiling a Pattern is to only do it once. It really depends on how you’re going to use it, but in general, pre-compiled patterns stored in static fields should be fine. (Unlike Matchers, which aren’t threadsafe and therefore shouldn’t really be stored in fields at all, static or not.) The … Read more
Instead of \w, use the POSIX bracket expression [:alpha:]: “blåbær dèjá vu”.scan /[[:alpha:]]+/ # => [“blåbær”, “dèjá”, “vu”] “blåbær dèjá vu”.scan /\w+/ # => [“bl”, “b”, “r”, “d”, “j”, “vu”] In your particular case, change the regex to this: NAME_REGEX = /^[[:alpha:]\s'”\-_&@!?()\[\]-]*$/u This does match much more than just accented characters, though. Which is a … Read more
Enum variants have three possible syntaxes: unit enum A { One } tuple enum B { Two(u8, bool) } struct enum C { Three { a: f64, b: String } } You have to use the same syntax when pattern matching as the syntax the variant was defined as: unit match something { A::One => … Read more
You’re almost there. Parentheses are required in order that the compiler interprets a let-bound as pattern matching: let (OrderId id) = orderId If orderId is a parameter of a function, you can also use pattern matching directly there: let extractId (OrderId id) = id
All you need to do is make your val side (left of the =) compatible with your initializer (right of the =): scala> val Array(x, y, z) = “XXX,YYY,ZZZ”.split(“,”) x: java.lang.String = XXX y: java.lang.String = YYY z: java.lang.String = ZZZ As you expected, a scala.MatchError will be thrown at runtime if the array size … Read more
The match/case syntax, otherwise known as Structural Pattern Matching, was only introduced in Python version 3.10. Note well that, following standard conventions, the number after the . is not a decimal, but a separate “part” of the version number. Python 3.9 (along with 3.8, 3.7 etc.) comes before Python 3.10, and does not support the … Read more
string[] xs = new[] { “h:/a/b/c”, “h:/a/b/d”, “h:/a/b/e”, “h:/a/c” }; string x = string.Join(“https://stackoverflow.com/”, xs.Select(s => s.Split(“https://stackoverflow.com/”).AsEnumerable()) .Transpose() .TakeWhile(s => s.All(d => d == s.First())) .Select(s => s.First())); with public static IEnumerable<IEnumerable<T>> Transpose<T>( this IEnumerable<IEnumerable<T>> source) { var enumerators = source.Select(e => e.GetEnumerator()).ToArray(); try { while (enumerators.All(e => e.MoveNext())) { yield return enumerators.Select(e => e.Current).ToArray(); … Read more