How do I grab multiple values in regex? - regex

I'm trying to filter out 3 sensu check values for templating.
I'm using Elasticsearch as a datasource
Query: {"find": "terms","field":"check_name.keyword"}
Regex: /.*_error_100.*|.*_error_200.*|.*_error_300.*/
Is my regex wrong?
Thank you
Devon

Matching everything like .* is very slow as well as using lookaround
regular expressions.
To query some field by regex (exemplary query):
{
"query": {
"regexp":{
"somefield": "_error_[123]00"
}
}
}
https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-regexp-query.html#regexp-syntax

Related

Regular Expressions in Elasticsearch/Kibana

I only want to retrieve events that match my regular expression for a particular field. For example, events that have an IP address. Elastic doesn't support PCRE so is there a way I can achieve this from their supported regular expression syntax?
Here is the regex I had before discovering it was not supported by Elastic:
https://regex101.com/r/99b6dn/3
Here is what I attempted using Elastic's supported syntax, but it's not working:
/[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*/
Expected result would be myfield:/<myregex>/ returning only logs that match the regex.
Only the non-capture group (?: ) in your pattern is absent from the list of supported regex operators. Try:
([0-9]{1,3}\.){3}[0-9]{1,3}
The query should look something like this:
{
"query": {
"match": {
"path": {
"query": "([0-9]{1,3}\.){3}[0-9]{1,3}",
"type": "phrase"
}
}
}
}
Or a more accurate pattern
((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)

What is the correct way to format regex metacharacters and options when using the regex operator in $searchBeta in MongoDB?

I'm trying to do full-text search in MongoDB with $searchBeta (aggregation) and I'm using the 'regex' operator to do so. Here's the portion of the $searchBeta I have that isn't working how I expecting it would:
$searchBeta: {
regex: {
query: '\blightn', // '\b' is the word boundary metacharacter
path: ["name", "set_name"],
allowAnalyzedField: true
}
}
Here's an example of two documents that I'm expecting to get matched by the expression:
{
"name": "Lightning Bolt"
"set_name": "Masters 25"
},
{
"name": "Chain Lightning",
"set_name": "Battlebond"
}
What I actually get:
[] //empty array
If I use an expression like:
$searchBeta: {
regex: {
query: '[a-zA-Z]'
path: ["name", "set_name"],
allowAnalyzedField: true
}
}
then I get results back.
I can't get any expression that has regex metacharacters and/or options in it to work, so I'm pretty sure I'm just entering it wrong in my query string. The $searchBeta regex documentation doesn't really cover how to format metacharacters into your query string. Also, the $searchBeta regex operator is different from $regex because it doesn't require slashes (i.e. "/your expression/" ). Really pulling my hair out on something so simple that I can't figure out.
$searchBeta uses Lucene for regular expressions, which is not Perl Compatible (PCRE) and doesn't support \b. You can read about the Lucene regex syntax here and also Elastic's docs on it are also helpful.
Here is a similar question for ElasticSearch and includes some workarounds.

MongoDB regex query from Spring Boot

I want to keep a regex in MongoDB document like
regexToApply : "someRegularExpression"
and I want to query with a string and get all matching documents from mongo.
I played with $regex, but it's not regular regex operation.It's kind of reverse regex operation. I tried to something like below, but it does not return any document.
#Query("{$where: ?0.$match('regexToApply')}")
List findByRegex(String myText);
You should be able to use $expr with $regexMatch
{$expr: {$regexMatch:{input:"string", regex:"$regexToApply"}}}

Extracting a value from JSON Response using Regular Expression Extractor in Jmeter

I have JSON response from which i want to extract the "transaction id" value i.e (3159184) in this case and use it in my next sampler. Can somebody give me regular expression to extract the value for the same. I have looked for some solutions but it doesn't seem to work
{
"lock_release_date": "2021-04-03T16:16:59.7800000+00:00",
"party_id": "13623162",
"reservation_id": "reserve-1-81b70981-f766-4ca7-a423-1f66ecaa7f2b",
"reservation_line_items": [
{
"extended_properties": null,
"inventory_pool": "available",
"lead_type": "Flex",
"line_item_id": "1",
"market_id": 491759,
"market_key": "143278|CA|COBROKE|CITY|FULL",
"market_name": "143278",
"market_state_id": "CA",
"product_name": "Local Expert",
"product_size": "SOV30",
"product_type": "Postal Code",
"reserved_quantity": 0,
"transaction_id": 3159174
}
],
"reserved_by": "user1#abc.com"
}
Here's what i'm trying in Jmeter
If you really want the regular expression it would be something like:
"transaction_id"\s?:\s?(\d+)
Demo:
where:
\s? stands for an optional whitespace - this is why your expression doesn't work
\d+ stands for a number
See Regular Expressions chapter of JMeter User Manual for more details.
Be aware that parsing JSON using regular expressions is not the best idea, consider using JSON Extractor instead. It allows fetching "interesting" values from JSON using simple JsonPath queries which are easier to create/read and they are more robust and reliable. The relevant JSON Path query would be:
$.reservation_line_items[0].transaction_id
More information: API Testing With JMeter and the JSON Extractor
Use JSON Extractor for JSON response rather using Regular Expression extractor.
Use JSON Path Expressions as $..transaction_id
Results:
Simplest Regular Expression for extracting above is:
transaction_id": (.+)
Where:
() is used for creating capture group.
. (dot) matches any character except line breaks.
+ (plus) matches 1 or more of the preceding token.
(.+?) could be used to stop looking after first instance is found.
i.e. ? makes the preceding quantifier lazy, causing it to match as few characters as possible. By default, quantifiers are greedy, and will match as many characters as possible.

Query document based on field's value containing backslash using regex

I'm trying to query DB with documments similar to one presented below.
{
"_id":"5b9bd1b947c7471038399a39",
"subdir":"ge\\pt02\\kr02_20180824\\kr02_2018091log\\0010796ab5",
}
How to filter all documments starting with: ge\\pt02\\kr02
I tried many different approaches,
for example:
{"subdir": {"$regex": "pt02\\kr02*"}}
but I cannot figure out how to prepare a correct filter:
The problem is that you need to escape the slashes.
Here is a working example:
db.test1.insert({"subdir":"ge\\pt02\\kr02_20180824\\k2_2018091log\\0010796ab5"})
db.test1.find({"subdir": { $regex: "^ge\\\\pt02\\\\kr02"}})
This prints out:
{ "_id" : ObjectId("5ba28194fbb45cb9f7c58b18"), "subdir" : "ge\\pt02\\kr02_20180824\\kr02_2018091log\\0010796ab5" }
We need to escape the backslash there. Also since you want to select only the documents starting with this pattern, you need to group the regex into a parenthesis and prefix the group with caret. This gives us the following regex:
let pattern = "^(ge\\\\pt02\\\\kr02)";
{"subdir": {"$regex": pattern}}
Demo: