How can I generate text from a RegEx? [duplicate] - regex

This question already has answers here:
Using Regex to generate Strings rather than match them
(12 answers)
Reverse regular expression, create string from regex
(1 answer)
Closed 9 years ago.
Or "How can I RegEx in reverse?"
specifically I want to take a regex such as wks[0-9][0-9][0-9]
and create a list such as wks001,wks002,wks003, etc
I know the easiest way in this example would be to simply increment the number through addition, but say I want it to be even more sophisticated later on such as [0-9abc] I'd like to use a more sophisticated tool.
preferable would be some windows capable scripting tech, such as vbscript/powershell but I'm open to other alternatives. I guess I kind of thought this might be something that is done all the time by random number generators and such and would be a programming staple, but I lack the understanding to phrase it correctly I think.

Related

Why using (.|\n)*? is a bad idea? [duplicate]

This question already has answers here:
How do I match any character across multiple lines in a regular expression?
(26 answers)
Regex search with pattern containing (?:.|\s)*? takes increasingly long time
(1 answer)
Closed 3 years ago.
A few times I saw regex experts say that using (.|\n)*? is a really, really bad idea.
Well, I do understand that it's better to replace it with the .* and use the /s flag. But sometimes the flags are not available, for example, when using regex within a text editor or other software with limited regex functionality. Thus, using something like (.|\n)*? might be the only option for multi-line matching.
So, what are the reasons to always avoid (.|\n)*??

Regex for all strings that contain exactly two occurrences of 'aa'? [duplicate]

This question already has answers here:
Find shortest matches between two strings
(4 answers)
Closed 4 years ago.
This is a simple question of the Theory of Computation.
I don't know nor want the python coded interpretation of this but rather the theoretical answer of the expression.
I have tried my best to figure it out and came up with the below code:
(ab+ba+bb)*. aa.(ab+ba+bb)*.aa.(ab+ba+bb)* + b*.aa.b*.aa.b*
Is it right? Am I forgetting any other case?
Your regex is too complicated and not very flexible (it only works with strings of a and b). A better solution uses negative look-ahead assertions:
^(?:(?!aa).)*aa(?:(?!aa).)*aa(?:(?!aa).)*$
This looks for any length of substring at the start of the string that does not contain aa, then the first aa, and so on.

How to extract body of for-loop? [duplicate]

This question already has answers here:
Can regular expressions be used to match nested patterns? [duplicate]
(11 answers)
Closed 6 years ago.
There are multiple C++ files. I need to extract the body of for-loop from these files.
Is there an easy way to do this maybe using grep. Consider there are no nested for loops.
Without parsing the entire file, the answer is no.
for-loops are comprised of a context-free grammar and, as such, cannot be matched by a regular expression.
A more involved approach is to use grep to search for the beginning of a for-loop (for follow by optional whitespace followed by a lpar) then manually find the closing curly.
Unfortunately parsing C++ is Turing Complete, so unless there's some cute flag to pass to your compiler, you're hosed.

MongoDB regex reverse [duplicate]

This question already has answers here:
MongoDB reverse regex
(2 answers)
Closed 4 years ago.
Ok, I know, there was a similar question on this site, but it's 2 years old, so the situation can be different now.
I have some documents with stored regular expressions, just like this:
{
_id: ObjectId("aaaaaaaaaaaaaaaaaaaaaaaa"),
regex: /^some_regex.*$/
}
So I've got an input string and I need to find all the documents, where regex matches the string.
The only way to solve the problem I know, is $where operator usage, but it's not a solution I'm looking for, because $where is very, very slow. Is there another way?
There is no such an opportunity in Mongo. $where operator is the only way to perform this server-side.

Is it possible to reliably determine if a given regular expression will match any string? [duplicate]

This question already has answers here:
Does an algorithm exist which can determine whether one regular language matches any input another regular language matches?
(4 answers)
Closed 9 years ago.
I'd like to take a user-input regular expression and determine whether or not it will match any string, i.e. would it "reduce" to .+ or .*?
I suspect that since this exists, that my question will reduce to the halting problem, but I'd really like to be wrong about that.
I don't think what you want is similar to the Halting problem since the grammar of regular expression. Considering the alphabet and the language recognized by your automaton is finite, you can still use a dummy algorithm that would try every world of your language and test if the regular expression is able to recognize it or not.
In practice, this method has an awful complexity but you don't have any "undefined" state you would have in Halting problem since the number of inputs is enumerable.
I actually don't know if a better version of this dummy algorithm exists, but i hope i answered about your question on similarity to the Halting problem.