I'm using Node.js and MongoDB, and I'm trying to perform a $search on a $text field from a collection. As mentioned in the docs, To match on a phrase, as opposed to individual terms, enclose the phrase in escaped double quotes (\"), as in:
"\"ssl certificate\""
I have a variable query that holds the value inputted by a user in a simple text search application. I want to add this query as a phrase, so that I can perform my search on all the words entered by the user appropriately.
Is there anyway one can achieve this?
assuming the user input is in the variable input you can add double qoutes around it like so:
input = '"'+input+'"';
this string, you should then be able to use in your $search.
If you post your code, I could try to give a more specific answer.
Instead of using regex of the format /regexString/g you can also create Regex objects using variables.
var regexExp = new RegExp(query,"g");
You can use this regexExp to search.
Related
I have already implemented angular multi-select Dropdown. Now I want it to search using RegEx. Like if I have qwertyuiop, and if I wrote w*i than it should suggest me all the entries who contains 'W' and 'I' in the same string.
Do you want to know the whole thing, like how to bind the input of an input field, read the input, create an RegEx out of it, and then use the RegEx as a filter to the list you are showing in your dropdown ?
Or only the RegEx part?
For the part with RegEx:
You should take a look at the Javascript Defintion of RegEx (for example at Mozilla Developer Network ). It has a quite nice functionality.
let input:string = // the value the user typed, like w*i
const regEx = new RegExp(input)
let myDropdownList:string[] = // the list of strings i want to filter
let filteredDropdownList = myDropdownList.forEach((value:string)=>{
return regEx.test(value)
})
What happens here?
You are creating a regular Expressen with new RegExp(someString).
You can optimize it with RegEx flags.
Later you test a string with myRegEx.test(theString). It will return true if the regEx founds at least one match in the string.
I hope this helps you a bit.
warm regards.
I am trying to replace substrings that look like the following
Education|AAA|BBB|CCC|DDD|EEE
AAA|Educator|CCC|DDD|EEE
where Education or Educator could be in any position of the pipe delimited string. They are present at most only one time or zero times
I need to replace them with Educator/Education
What I want is
Educator/Education|AAA|BBB|CCC|DDD|EEE
AAA|Educator/Education|CCC|DDD|EEE
This works on the first pass with Education, I get
Educator/Education|AAA|BBB|CCC|DDD|EEE
AAA|Educator|CCC|DDD|EEE
But on the second pass replacing Educator, I get
Educator/Education/Education|AAA|BBB|CCC|DDD|EEE
AAA|Educator/Education|CCC|DDD|EEE
I think different a RegEx pattern for the ReplaceNth function would do it but cannot get what it should be
This is what I came up with in my limited ability with Google Script, which I got mostly from cobbling together from searches
Thanks
Edit: Updated with Wiktors solution given in a comment
function FRCrit_n(){
FRCrit("Elements", "Combined-What Are The Primary Role(s) Being Play?");
}
function FRCrit(shtName,cheader){
var sh = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(shtName);
var data = sh.getDataRange().getValues();
var col =HTN(shtName,cheader)-1; //Header to column index
for(n=1;n<data.length;++n){
data[n][col] = data[n][col].replace(/\bEducat(?:or|ion)\b/g, 'Educator/Education')[col],"Education","Educator/Education",1)
}
sh.getRange(1,1,data.length,data[0].length).setValues(data);
}
In regular expression syntax, you can create an 'or' condition using a vertical bar to search for multiple things in one go. You can test this out in G Suite Sheets without needing any code.
I think you want to search for
Education|Educator
and replace it with
Education/Educator
When you are happy with your results you can put it back into code - the same regex syntax should work in javascript.
When I post a value from my page an extra string is create and I would like to remove the 'pattern = "' is there a tag i could use or replace function i can use to remove pattern =" and the ending ". Please find below scenario:
pattern = "apple"
Desired output
apple
I tried using but to no success.Is there another method i could use newbie at django python.
{{ pattern|split }}
Write a custom templatetag and use a regular expression (a capture group would do the trick) to replace the desired part.
If the quotation marks are added to your string when you are submitting the form, you could use the .strip() method when accessing the POST data. You can also specify what characters you want to remove. Check it out here: http://docs.python.org/2/library/stdtypes.html
I made an article spinner that used regex to find words in this syntax:
{word1|word2}
And then split them up at the "|", but I need a way to make it support tier 2 brackets, such as:
{{word1|word2}|{word3|word4}}
What my code does when presented with such a line, is take "{{word1|word2}" and "{word3|word4}", and this is not as intended.
What I want is when presented with such a line, my code breaks it up as "{word1|word2}|{word3|word4}", so that I can use this with the original function and break it into the actual words.
I am using c#.
Here is the pseudo code of how it might look like:
Check string for regex match to "{{word1|word2}|{word3|word4}}" pattern
If found, store each one as "{word1|word2}|{word3|word4}" in MatchCollection (mc1)
Split the word at the "|" but not the one inside the brackets, and select a random one (aka, "{word1|word2}" or "{word3|word4}")
Store the new results aka "{word1|word2}" and "{word3|word4}" in a new MatchCollection (mc2)
Now search the string again, this time looking for "{word1|word2}" only and ignore the double "{{" "}}"
Store these in mc2.
I can not split these up normally
Here is the regex I use to search for "{word1|word2}":
Regex regexObj = new Regex(#"\{.*?\}", RegexOptions.Singleline);
MatchCollection m = regexObj.Matches(originalText); //How I store them
Hopefully someone can help, thanks!
Edit: I solved this using a recursive method. I was building an article spinner btw.
That is not parsable using a regular expression, instead you have to use a recursive descent parser. Map it to JSON by replacing:
{ with [
| with ,
wordX with "wordX" (regex \w+)
Then your input
{{word1|word2}|{word3|word4}}
becomes valid JSON
[["word1","word2"],["word3","word4"]]
and will map directly to PHP arrays when you call json_decode.
In C#, the same should be possible with JavaScriptSerializer.
I'm really not completely sure WHAT you're asking for, but I'll give it a go:
If you want to get {word1|word2}|{word3|word4} out of any occurrence of {{word1|word2}|{word3|word4}} but not {word1|word2} or {word3|word4}, then use this:
#"\{(\{[^}]*\}\|\{[^}]*\})\}"
...which will match {{word1|word2}|{word3|word4}}, but with {word1|word2}|{word3|word4} in the first matching group.
I'm not sure if this will be helpful or even if it's along the right track, but I'll try to check back every once in a while for more questions or clarifications.
s = "{Spinning|Re-writing|Rotating|Content spinning|Rewriting|SEO Content Machine} is {fun|enjoyable|entertaining|exciting|enjoyment}! try it {for yourself|on your own|yourself|by yourself|for you} and {see how|observe how|observe} it {works|functions|operates|performs|is effective}."
print spin(s)
If you want to use the [square|brackets|syntax] use this line in the process function:
'/[(((?>[^[]]+)|(?R))*)]/x',
I use the following regular expression to validate a comma separated list of values.
^Dog|Cat|Bird|Mouse(, (Dog|Cat|Bird|Mouse))*$
The values are also listed in a drop down list in Excel cell validation, so the user can select a single value from the drop down list, or type in multiple values separated by commas.
The regular expression does a good job of preventing the user from entering anything but the approved values, but it doesn't prevent the user from entering duplicates. For example, the user can enter "Dog" and "Dog, Cat", but the user can also enter "Dog, Dog".
Is there any way to prevent duplicates using a similar single regular expression? In other words I need to be able to enforce a discrete list of approved comma separated values.
Thanks!
Use a backreference and a negative lookahead:
^(Dog|Cat|Bird|Mouse)(, (?!\1)(Dog|Cat|Bird|Mouse))*$
EDIT: This won't work with cases such as "Cat, Dog, Dog" ... You'll need to come up a hybrid solution for such instances - I don't believe there is a single regex that can handle that.
Here's another technique. You need to check two things, first, that it DOES match this:
(?:(?:^|, )(Dog|Cat|Bird|Mouse))+$
(That's just a slightly shorter version of your original regex)
Then, check that it DOES NOT match this:
(Dog|Cat|Bird|Mouse).+?\1
E.g.
var valid = string.match( /(?:(?:^|, )(Dog|Cat|Bird|Mouse))+$/ ) &&
!string.match( /(Dog|Cat|Bird|Mouse).+?\1/ );
J-P, I tried editing your sample regular expressions so that I could look for duplicates in any comma separated string. Something like this:
var valid = string.match( /(?:(?:^|, )([a-z]*))+$/ ) &&
!string.match( /([a-z]*).+?\1/ );
Unfortunately, I failed. The Force is weak with me. ;)
Thanks again for your help.
What about using some kind of expression like this:
(Dog|Cat|Bird|Mouse){1}
Then you can write only a value from the aray once. It is easy then to add zero or more times, commas, spaces, etc.
I know i'm necroposting but i found this in my search, so i'll let it sit here if anyone will find it.