'How to find the exact word using a regex in Java?
Consider the following code snippet:
String input = "Print this";
System.out.println(input.matches("\\bthis\\b"));
Output
false
What could be possibly wrong with this approach? If it is wrong, then what is the right solution to find the exact word match?
PS: I have found a variety of similar questions here but none of them provide the solution I am looking for. Thanks in advance.
Solution 1:[1]
Full example method for matcher:
public static String REGEX_FIND_WORD="(?i).*?\\b%s\\b.*?";
public static boolean containsWord(String text, String word) {
String regex=String.format(REGEX_FIND_WORD, Pattern.quote(word));
return text.matches(regex);
}
Explain:
- (?i) - ignorecase
- .*? - allow (optionally) any characters before
- \b - word boundary
- %s - variable to be changed by String.format (quoted to avoid regex errors)
- \b - word boundary
- .*? - allow (optionally) any characters after
Solution 2:[2]
For a good explanation, see: http://www.regular-expressions.info/java.html
myString.matches("regex") returns true or false depending whether the string can be matched entirely by the regular expression. It is important to remember that String.matches() only returns true if the entire string can be matched. In other words: "regex" is applied as if you had written "^regex$" with start and end of string anchors. This is different from most other regex libraries, where the "quick match test" method returns true if the regex can be matched anywhere in the string. If myString is abc then myString.matches("bc") returns false. bc matches abc, but ^bc$ (which is really being used here) does not.
This writes "true":
String input = "Print this";
System.out.println(input.matches(".*\\bthis\\b"));
Solution 3:[3]
You may use groups to find the exact word. Regex API specifies groups by parentheses. For example:
A(B(C))D
This statement consists of three groups, which are indexed from 0.
- 0th group - ABCD
- 1st group - BC
- 2nd group - C
So if you need to find some specific word, you may use two methods in Matcher
class such as: find()
to find statement specified by regex, and then get a String
object specified by its group number:
String statement = "Hello, my beautiful world";
Pattern pattern = Pattern.compile("Hello, my (\\w+).*");
Matcher m = pattern.matcher(statement);
m.find();
System.out.println(m.group(1));
The above code result will be "beautiful"
Solution 4:[4]
Is your searchString
going to be regular expression? if not simply use String.contains(CharSequence s)
Solution 5:[5]
System.out.println(input.matches(".*\\bthis$"));
Also works. Here the .* matches anything before the space and then this is matched to be word in the end.
Solution 6:[6]
Well it seems like you're updating single object. That is why you're taking pk from url. So very first change you need here is .
@api_view(['PUT'])
def productupdate(request,pk):
data = request.data
prod = Products.objects.get(id=pk)
serializer = Productserialize(prod,data=data, many=True)
if serializer.is_valid():
serializer.save()
return Response(serializer.data)
change that many=True
to many=False
.
and you can also comment .
def update(self, instance, validated_data):
"""
Update and return an existing `Snippet` instance, given the validated data.
"""
instance.id = validated_data.get('id', instance.id)
instance.title = validated_data.get('title', instance.title)
instance.description = validated_data.get('description', instance.description)
instance.image = validated_data.get('image', instance.image)
instance.save()
return instance
from your serializer. Because your views are already handling that updating logic..
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Enlico |
Solution 2 | Paolo Falabella |
Solution 3 | Alan Moore |
Solution 4 | Kuldeep Jain |
Solution 5 | edwin gnanasigamony |
Solution 6 | dummy first |