How do I check a string for certain words without getting matches for parts of the words in the string in Python?
By : Ravikumar P
Date : March 29 2020, 07:55 AM
wish of those help One way to accomplish this would be to split your string into a list, then check if each word is in the list: code :
string_list = string.split(' ')
for word in wordList:
if word in string_list:
print word
for word in wordList:
posMatch = re.search(r'\b%s\b' % word, string)
if posMatch:
print (posMatch.group())
|
How to match words in 2 list against another string of words without sub-string matching in Python?
By : DizzyKnight
Date : March 29 2020, 07:55 AM
this one helps. Store slangNames and riskNames as sets, split the strings and check if any of the words appear in both sets code :
slangNames = set(["Vikes", "Demmies", "D", "MS", "Contin"])
riskNames = set(["enough", "pop", "final", "stress", "trade"])
d = {1: "Vikes is not enough for me", 2:"Demmies is okay", 3:"pop a D"}
for k,v in d.items():
spl = v.split() # split once
if any(word in slangNames for word in spl) and any(word in riskNames for word in spl):
print(k,v)
1 Vikes is not enough for me
3 pop a D
slangNames = set(["Vikes", "Demmies", "D", "MS", "Contin"])
riskNames = set(["enough", "pop", "final", "stress", "trade"])
d = {1: "Vikes is not enough for me", 2:"Demmies is okay", 3:"pop a D"}
for k,v in d.items():
spl = v.split()
if not slangNames.isdisjoint(spl) and not riskNames.isdisjoint(spl):
print(k, v)
import re
slangNames = set(["Vikes", "Demmies", "D"])
r = re.compile(r"\bMS Contin\b")
riskNames = set(["enough", "pop", "final", "stress", "trade"])
d = {1: "Vikes is not enough for me", 2:"Demmies is okay", 3:"pop a D"}
for k,v in d.items():
spl = v.split()
if (not slangNames.isdisjoint(spl) or r.search(v)) and not riskNames.isdisjoint(spl):
print(k,v)
|
Take a String with any number of words and store the words in different string variables?
By : linklux
Date : March 29 2020, 07:55 AM
this one helps. Since you're already using the standard library, why not use a vector? code :
#include <iostream>
#include <sstream>
#include <vector>
#include <string>
std::string input = "abc def ghi";
std::istringstream ss(input);
std::string token;
std::vector<std::string> vec;
while(std::getline(ss, token, ' ')) {
vec.push_back(token);
}
//vec now contains ['abc', 'def', 'ghi']
|
How to compare words of one string in Java with words of another string and separate the words on match?
By : petrt88
Date : March 29 2020, 07:55 AM
it should still fix some issue The issue you're having (and the reason the first string succeeded and the second does not) is to do with the order of words in the dict. Your current implementation checks if the words in the dict appear in the string exactly in the order they were entered into the dict - after you found the first word, put in a space and proceed to find the second word. If you did not find the next word, you do not proceed with the process. There are many ways to rewrite the code to get what you want, but the minimal change is: code :
public class Sample1 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int i,j,k,len;
String[] dict= {"how","are","you","something","needs","to","be","done"};
//StringBuilder str=new StringBuilder("howareyou");
StringBuilder str=new StringBuilder("somethingneedstobedone");
len=str.length();
for(i=0,j=0;i<len;i++) //removed k from here
{
for(j=i+1;j<len;j++)
{
for (k=0;k<dict.length;k++) { //added this loop!
if(dict[k].toString().equals(str.substring(i, j)))
{
str.insert(j, " ");
}
} //Loop closing for k - the dictionary
}
}
System.out.println(str);
sc.close();
}
|
How to compare string with array of words and highlight words in string that match?
By : Gazeld
Date : March 29 2020, 07:55 AM
it should still fix some issue Convert the array to a regular expression, and use String#Replace to wrap the words with a span: code :
const words = ['prueba', 'etiquetas'];
const product = 'Prueba Pruebaa de etiquetas aetiquetas';
// convert the array to a regular expression that looks for any word that is found in the list, regardless of case (i), over all the string (g)
const regexp = new RegExp(`\\b(${words.join('|')})\\b`, 'gi');
// replace the found words with a span that contains each word
const html = product.replace(regexp, '<span class="highlight">$&</span>');
demo.innerHTML = html;
.highlight {
background: yellow;
}
<div id="demo"></div>
|