Regular expression for wildcard word collection

0

I'm stuck in the following situation: I have a dictionary of words in a mongodb bank, where each word is stored in a document, which has additional information about it.

I need a regular expression that can find each document through the word, since some words in the database have a wildcard at the end (*). When this occurs, the wildcard word must be found if it is at the beginning of the search string. It is a kind of regular expression in the database, to simplify the word registration. Thus, it suffices that the document has only the necessary part of the word with the wildcard at the end so that it is returned when the search string "marries" with the word. It would be impracticable to register every possible word, and to be completely unnecessary, since many words have exactly the same additional information.

To make the situation clearer, here are some examples:

  • If the word in the database is "asked", the document should be returned whenever the search string starts with "ask" (question, ask, ask, etc.)
  • If the word in the database is "amig *", the document should be returned whenever the search string starts with "friend" (friend, friend, etc.)
  • If the word in the database is "love," the document should be returned whenever the search string is exactly "love."

I need a single regular expression that will serve all of the above (wildcard at the end, no wildcard).

If possible, also show a solution for cases where the wildcard is also at the beginning of the word (* word *), in this case, a same regular expression for all situations (wildcard at the beginning, wildcard at the end, wildcard at the beginning and in the end, no wildcard).

I thank you in advance. It will help me a lot.

    
asked by anonymous 13.11.2018 / 02:35

1 answer

0

Do you want the user to send "question" and want it to become a regular expression that validates "ask *"? This does not make sense, to build a regular expression you need to know the format of what you are validating, "question" does not contain information for this.

If it were to exist a regular expression in this process, it was to exist registered in the database. For example, if the expression /^pergunt/i was registered, the user could send "question", "ask", "asked", and all those words would be valid.

/^amor$/i would validate "love" but not "love", /palavra/i would validate any string containing "word" in the beginning, middle or end.

    
13.11.2018 / 04:44