Tool to generate regex from sample?
java - Regex to String generation - Software Engineering Stack Exchange
Regex generator
Regex Generator?
What is regex in AI?
What programming languages work with the generated regex?
Can AI write regex patterns for me?
Videos
I know of solutions like regex101, that enable to test a regex. It's still something that requires time to figure out one's needed regex to start with π
Wondering if there is a tool that enables you to provide a sample string, indicating what part of the string you would want to extract, that will generate the relevant regex? π€
Β» npm install regex-to-strings
A regular expression maps directly to a finite state machine. Generating strings from the FSM is straightforward with a backtracking algorithm. The example is for the regular expression /aa?b/
(start) 0 -- a --> 1 -- a --> 2 -- b --> 3 (accepting and terminal)
| ^
|---------b ----------|
State Previous char Input char Input string History
0 null -- []
a a
1 null [{a,1}]
a aa [{a,1},{a,2}]
2 null
b aab [{a,1},{a,2},{b,3}]
3 null
Accepting state, emit 'aab' as valid input
Terminal state, backtrack
2 b aa [{a,1},{a,2}]
No further valid inputs, backtrack
1 a a [{a,1}]
b ab
3 null [{a,1},{b,3}]
Accepting state, emit 'ab' as valid input
Terminal state, backtrack
1 b a [{a,1}]
No further valid inputs, backtrack
0 a -- []
No further valid inputs, stack empty, terminate
On their own, regexes are serving the purpose of answering the question: "Given this list of strings as input and this pattern, which subset mathces it?". It does not work backwards i.e. from this regex, start generating strings that match it.
Having said that, take a look at this: https://code.google.com/p/xeger/, that is supposed to do what you want to achieve (with some limitations as described in https://code.google.com/p/xeger/wiki/XegerLimitations).
If your regex is accepted, I guess the anwer to your question is pretty straightforward i.e. generate the list and take the item at index i