I have field in mongoDb collection "name" which contains:
"26.11.2018(2)"
I use regex expression for searching if any string contains in field "name":
String search = "11.2018(2)";
return Criteria.where("name").regex(search);
I got exception, that regular expression is wrong(because of ")"). Is there any other possibility for searching like this?
You need to escape the value that's used in the regex.
I can't test it now, but it's possible that it may work:
String search = "some pattern(a)12.";
pattern = Pattern.compile(Pattern.quote(search), Pattern.CASE_INSENSITIVE);
return Criteria.where("name").regex(pattern);
https://mongodb.github.io/mongo-java-driver/3.4/javadoc/?com/mongodb/client/model/Filters.html
Related
I've added #Pattern annotation to a query parameter in my rest controller (SpringBoot Kotlin). I would like the regex in the pattern to accept -
optionA or optionB or null (nothing/an empty string)
The following works, but of course does not include the empty option -
#Pattern(regexp = "(?i)optionA||(?i)optionB")
This does not work -
#Pattern(regexp = "(?i)optionA||(?i)optionB||^\\s*\$")
Can anybody help me with this? :)
Thanks!
Inside the #Pattern annotation, the pattern is used to match the entire string, so you can use
#Pattern(regexp = "(?i)(?:optionA)?")
which is actually \A(?i)(?:optionA)?\z:
\A - start of string (implicit here)
(?i) - case insensitive embedded flag option
(?:optionA)? - an optional non-capturing group that matches optionA or empty string
\z - end of string (implicit here).
The null is a memory address, not a string, it cannot be matched with regex, as regex only deals with strings.
I tryed this optionA|optionB|^\s$ on https://regex101.com/ and worked well. Could you try on your app to check?
I'm using Elasticsearch 3.2.7 and ElasticsearchRepository.search() which takes QueryBuilder as an argument (doc)
I have a BoolQueryBuilder and use it like this:
boolQuery.must(termQuery("myObject.code", value);
var results = searchRepository.search(boolQuery);
The definition of the field code is as follows:
"myObject": {
"properties": {
"code": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
The issue is, when I search with a value that has underscore inside, for example: FOO_BAR then it doesn't return any results. When I search with other values that have either leading or trailing underscore then it's fine.
I've read that ES may ignore the special character and split the words inside by it so there's a need for an exact match search. But I also read that the keyword setting guarantees that. So right now I'm confused.
yes, you are correct, using keyword field you can achieve the exact match, you need to use the below query
boolQuery.must(termQuery("myObject.code.keyword", value); --> note addition of keyword
var results = searchRepository.search(boolQuery);
you can use the analyze API to see the tokens for your indexed documents and search term, and basically your tokens in index must match search terms tokens, in order ES to return the match :)
I receive a user input keyword and want to use it to search my database. I built a query that looks something like this:
db.execute("MATCH (n:User) WHERE n.firstname CONTAINS {keyword} OR n.lastname CONTAINS {keyword} RETURN n.username", params);
But this isn't case sensitive, so I thought of manually building the expression and using regular expressions, sort of as follows:
db.execute("MATCH (n:User) WHERE n.firstname =~ '(?i).*" + keyword + ".*' OR n.lastname =~ '(?i).*" + keyword + ".*' RETURN n.username");
I'm looking either for a function for escaping the regex or a better solution for making the query case-insensitive. Any ideas?
I would suggest storing the properties as all lowercase (or uppercase) and then using the Cypher lower() function to convert user input to lowercase for comparison.
Add lowercase name properties
MATCH (n:User)
SET n.lowerFirstName = lower(n.firstname),
n.lowerLastName = lower(n.lastname)
Find lower case matches based on user input
db.execute("MATCH (n:User) WHERE n.lowerFirstName CONTAINS lower({keyword}) OR n.lowerLastName CONTAINS lower({keyword}) RETURN n.username", params);
I am trying to extract the last value from the string in Jmeter Regular Expression extractor.
My string
Server.init("asdfasd4ffffasdf", "http://x.x.x.x:8888/", "asdf-U-Yasdf77asdf99");
I want to get only asdf-U-Yasdf77asdf99.
I tried something like the below, but not correct:
Server.init\(".+", ".+", "([A-Za-z0-9\-]+)"\);
Using JMeter you need to reference your match group.
Reference Name: MYWORD
Regular Expression: Server\.init\("[^"]+", "[^"]+", "([^"]+)"\);
Template: $1$
Your captured match can be accessed by using ${MYWORD}
If you specify using a Match No: above, use the corresponding value to access the match.
That regex, while not very beautiful, should work when used correctly. But you need to look at the result of group 1, not the entire match.
So you need to do something like
Pattern regex = Pattern.compile("Server\\.init\\(\"[^\"]+\", \"[^\"]+\", \"([A-Za-z0-9\\-]+)\"\\);");
Matcher regexMatcher = regex.matcher(subjectString);
if (regexMatcher.find()) {
ResultString = regexMatcher.group(1);
}
Regular Expression:
Server.init\("(.+?)",\s"(.+?)",\s"(.+?)"
Matches the following string
Server.init("asdfasd4ffffasdf", "http://x.x.x.x:8888/", "asdf-U-Yasdf77asdf99"
we can extract the following values in jmeter:
$1 values = asdfasd4ffffasdf
$2 values = http://x.x.x.x:8888/
$3 values = asdf-U-Yasdf77asdf99
I am new to regex parsing in java. I want to parse the string which contain the records. But I want to select the selected part of that record only.
\"6\":\"Services Ops\",\"practice_name\":\"Services Ops\",\"7\":\"Management\",
For this, I have written regex expression as
(^\\\"6\\\":\\\"[A-Za-z \s]*)
and above expression gives me result as : \"6\":\"Services Ops\
I want only Service Ops
And also there are multiple records like \"5"\:\"xxx"\ and so on thus if I write the expression for only Service Ops then entries from other fields are also included in the result of the expression.
Is there any way that we can select the string which start with some pattern but we can exclude that pattern.
Like in above example, string starting with \"6\":\" but we can exclude this part and get only Service Ops as result.
Thank you.
You can use lookarounds which perform only a check but don't match:
lookahead (?=...)
lookbehind(?<=...)
example:
(?<=\\\"6\\\":\\\")[^\"]++(?=\")
An another way is to use a capturing group (...):
\\\"6\\\":\\\"([^\"]++)\"
Then you can extract only the content of the group. Example:
Pattern p = Pattern.compile("\\\"6\\\":\\\"([^\"]++)\"");
Matcher m = p.matcher(yourString);
if (m.matches()) {
System.out.println(m.group(1));
}