I got the 2 texts:
First one:
My favorite programming language is c++.
Second one:
My favorite programming language is c.
and want to seek for c
and c++
in those texts separately.
For finding c
I can write: \bc\b
then: first text is bad! and second one is good. I tried also: \bc^\+\b
but doesn't work.
For fiding c++
I tried for example: \bc\+\+\b
but then first and second doesn't work. Help please.
EDIT:
And what if the text will be I programme in c++ a lot!
?
EDIT:
Here is the unit test which I need to fulfill:
package adhoc;
import java.util.HashSet;
import java.util.Set;
import org.junit.Test;
import junit.framework.TestCase;
public class FinderProgrammingTechnologyInTextTest extends TestCase{
@Test
public void testFind() {
// Given:
Set<String> setOfProgrammingLanguagesToSeek = new HashSet<>();
setOfProgrammingLanguagesToSeek.add("java");
setOfProgrammingLanguagesToSeek.add("perl");
setOfProgrammingLanguagesToSeek.add("c");
setOfProgrammingLanguagesToSeek.add("c++");
// When:
FinderProgrammingTechnologyInText finder = new FinderProgrammingTechnologyInText(
setOfProgrammingLanguagesToSeek);
Set<String> result = finder.find("java , perl! c++ and other staff");
// Then:
assertTrue(result.contains("java"));
assertTrue(result.contains("perl"));
assertFalse(result.contains("c"));
assertTrue(result.contains("c++"));
}
}
by changing ONLY the argument for compile()
method:
package adhoc;
import java.util.HashSet;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
public class FinderProgrammingTechnologyInText {
Set<String> setOfTechnologiesToSearch;
public FinderProgrammingTechnologyInText(Set<String> x) {
this.setOfTechnologiesToSearch = x;
}
public Set<String> find(String text) {
Set<String> result = new HashSet<>();
return setOfTechnologiesToSearch.stream()
.filter(x -> Pattern
.compile(x) // change only this line
.matcher(text).find()
)
.collect(Collectors.toSet());
}
}