MongoDb Select Query Issue Whit Regular Expression (Starts whit and Ends Whit) - regex

I need a regular expression to validate a String like this:
1678;1678;1678;1678 and 1;0;1;1;0;1
I tried to use this pattern:
db.getCollection('CollectionName').find(
{
"magnitude": /^[1678][1678]$/,
"flag": /^[1][1]$/
}
)
but it doesnt works, i try this two patterns that works separate but not both at the same time
db.getCollection('CollectionName').find(
{
"magnitude": /[1678]$/,
"flag": /[1]$/
}
)
db.getCollection('CollectionName').find(
{
"magnitude": /^[1678]/,
"flag": /^[1]/
}
)
I didnt find any character like * in SQL to use in this
I am using robomongo 1.0.0 for querys
I will apreciate any help
Thanks in advance

If you want to match more than one ; separated strings then use capturing groups.
db.getCollection('CollectionName').find(
{
"magnitude": /^1678(;1678)*$/,
"flag": /^[01](;[01])*$/
}
)
(;1678)* matches the string ;1678, zero or more times.
[01] matches either 0 OR 1

Related

Finding and replacing key: value pairs

I'm in the process of porting over a Python library to JavaScript / TypeScript. To help myself out, I'm trying to develop various regex rules that I can apply to files that will automatically convert a lot of the syntax and at least get me close, cleaning up where needed.
I've got the following example:
https://regex101.com/r/mIr0pl/1
this.mk(attrs={keyCollection.key: 40}))
this.mk(attrs={keyCollection.key: 50, override.key: override.value})
this.mk(attrs={keyCollection.key: 60,
override.key: override.value})
I am trying to do a Find/Replace in my editor, to find all key: value pairs associated with attrs dictionaries. Here's the regex I've got:
/attrs={(.+?):\s*(.+?)}/gms
I want to convert it to this:
this.mk(attrs=[[keyCollection.key, 40]]))
this.mk(attrs=[[keyCollection.key, 50], [override.key, override.value]])
this.mk(attrs=[[keyCollection.key, 60],
[override.key, override.value]])
I'm having trouble first nailing down the regex to get the repeated key: value groups, and then also how I would go about utilizing those repeated groups in a replace.
(my editor is VSCode, but I'm using this nifty extension to run these modifications: https://marketplace.visualstudio.com/items?itemName=bhughes339.replacerules)
Any help would be appreciated :)
Since VS Code already supports infinite-width lookbehind construct you may use
"replacerules.rules": {
"Wrap the attrs with square brackets first": {
"find": "(attrs=){([^:{]+:*[^}]*)}",
"replace": "$1[[$2]]"
},
"Format attributes inside attrs": {
"find": "(?<=attrs=\\[\\[[^\\]]*(?:](?!])[^\\]]*)*),(\\s*)",
"replace": "],$1["
},
"Replace colons with commas inside attrs": {
"find": "(?<=attrs=\\[\\[[^\\]]*(?:](?!])[^\\]]*)*):",
"replace": ","
}
}
"replacerules.rulesets": {
"Revamp attrs": {
"rules": [
"Wrap the attrs with square brackets first",
"Format attributes inside attrs",
"Replace colons with commas inside attrs"
]
}
}
Step #1 regex demo
Step #2 regex demo
Step #3 regex demo
Output:
this.mk(attrs=[[keyCollection.key, 40]]))
this.mk(attrs=[[keyCollection.key, 50], [override.key, override.value]])
this.mk(attrs=[[keyCollection.key, 60],
[override.key, override.value]])
Maybe,
(?<=attrs={|,)([^:}]*):([^:},]*)(?=}|,)
might be somehow closer.
If you might have had other attrs, you might want to initially filter out those others.
If you wish to explore/simplify/modify the expression, it's been
explained on the top right panel of
regex101.com. If you'd like, you
can also watch in this
link, how it would match
against some sample inputs.

vscode regex sub match evaluate instead of concatenate?

Test 300
Test 301
Test 302
I can use regex find to loop through these:
Test (3[0-9]*)
When I try replace with math it concatenates instead of evaluates?
Test $1-100
So, it becomes:
Test 300-100
Is it possible to evaluate instead of concatenate, so it becomes:
Test 200
Thanks.
You can use the VS Code Super Replace extension to achieve this.
Find field is the regular expression
Replace is the replace expression. Sub match with $$index syntax will be resolved using the function in Processing function field
Here is an example of use that answers your question :
There are more extensions that can do this now, including one I wrote Find and Transform.
With this keybinding:
{
"key": "alt+m", // whatever keybinding you want
"command": "findInCurrentFile",
"args": {
"find": "(?<=Test\\s)(3\\d\\d)", // get 300+ in capture group 1
"replace": "$${ return $1 - 100 }", // subtract 100 from capture group 1
"isRegex": true
}
}

Logstash - Change Alphabetic Month to Numeric

I have been trying to develop the Regex to convert alphabetic month (ex. Sep) to numeric equivalent (09)
The translate{} filter would work well for this.
using the datefilter with the MMMpattern will work fine for that:
filter {
date {
match => [ "yourfield", "MMM" ]
}
}
Available Regexes can be found on the DateTimeFormat documentation.

Regex get between 2 string with spaces

how could I parse this response text using Regex?
info = {
"title": "Developers",
"image": "http://i.ytimg.com/vi/KMU0tzLwhbE/default.jpg",
"length": "3",
"status": "serving",
"progress_speed": "",
"progress": "",
"ads": "",
"pf": "http://70efd.pf.aclst.com/ping.php/10754233/KMU0tzLwhbE?h=882634",
"h": "87d0670f6822946338a610a6b9ec5322",
"px": ""
};
The outcome I need should look like this "87d0670f6822946338a610a6b9ec5322", however, I can't get the correct syntax. I'm new to using Regex and what I have tried using is "\s+", can anyone point me in the right direction?
If you must use a regex, you could use a regex along the lines of:
"h" : "(.+?)",
You can see an example of it here. Just read from the first capture group and that would select your text.
That looks like like JSON aside from the info = prefix. If you have any specific language you are working in that could parse JSON, that might be a better way of handling that input.
You could also use (?<="h": ")[a-z0-9]+(?="), which will match any sequence of lowercase letters and numbers, as long as the sequence is preceded by "h": " and followed by ". I made an explanation and demonstration here.

JMeter: How to use Regular Expression to extract the value of a duplicate field?

I have the following Response Body JSON:
{
"address": [
{
"id": "1234"
}
],
"id": "d1a4f010-48d9-434b-9b3a-2d2b12f5e38c"
}
I am trying to extract the value of the the second "id" field, i.e.
"id": "d1a4f010-48d9-434b-9b3a-2d2b12f5e38c"
I use this Regular Expression in JMeter:
Regular Expression: "id":"(.+?)"
When I run my test, it returns "1234" instead of the guid. How can I change my Regular Expression so that it returns the guid?
Using JMeter, you can use Regular Expression Extractor ...
Reference Name: myid
Regular Expression: "id": "(.+?)"
Template: $1$
Match No.: 2
If you specify using a Match No:...
0 = Random Match
1 = First Match
2 = Second Match
etc....
Or use corresponding variable to access the match. ${myid_2}
The variables are set as follows:
myid_matchNr - Number of matches found, possibly 0
myid_n - (n = 1, 2, etc..) Generated by the template
myid_n_gm - (m = 0, 1, 2) Groups for the match (n)
myid - By itself it is always set to the default value
myid_gn - Not set at all
Or judging by this case, if you prefer just regex and your strings are exactly as stated. You could do..
],\s+"id": "(.+?)"
You can use a lazy regex to find the guid directly instead of finding "id"
Something like this: ([0-9a-z-]{36}).*?
If you are not sure how to create the regex, just use an online regex maker.
I don't know jmeter but to get the value of second id this expression
"id"\s*:.+?"id"\s*:\s*"([^"]*)"
It will return what you want on the $1 variable in a some languages. I suppose in jmeter you can have something similar to this in order to get the first group.
Maybe ${MYREF_g1} according this page ?
The given response values
{
"address": [
{
"id": "1234"
}
],
"id": "d1a4f010-48d9-434b-9b3a-2d2b12f5e38c"
}
Regular Expression to extract the second id values
Regular Expression formats
],\s\s\s\s\s"id": "(.+)"
Note
The above regex extract the following id values
d1a4f010-48d9-434b-9b3a-2d2b12f5e38c
I tried with this:
"id":\s*"([0-9a-f\-]*)"
try with below expression
"id": "([0-9a-z-])*+"
step 1:
id:"1234";
id:(.+?)
"1234"
But we need only the value. So try this,
Step 2:
id:"1234";
id:('(.+?)')
1234