How to use RegEx to determine which character is allowed next

How can I use Regex to determine which character is allowed as the next input?

I want to process serial input from the user and allow or deny the input of the next character, regardless of whether it is legal or not.

For example, for the following RegEx: ^\d{0,1}\d:\d\d$it corresponds to the type line 12:34, 1:23it works, if you set the entire line, but if the symbols are drawn one by one. I cannot determine if the substring matches the regular expression one way or another.

Given a substring 1, I would like to determine what the next character should be [0-9]or :.

How can this be achieved?

Thanks for any answers!

+4
source share
2 answers

Thanks to everyone for the answers, I learned something new about regex, but that did not help my needs. Maybe I was not specific enough in this matter.

I really wanted to process the sequences sequentially. I need a regex engine where I can pass in an arbitrary regex pattern and query if the next user input is valid (based on all previous inputs), and I would like to get the character set that is possible for the next char for auto-completion mechanisms.

//pseudo code

void main(string[] args){
    Regex regex = new Regex("^1(2|3)4$");
    RegexProcessor processor = new RegexProcessor(regex);

    bool step1 = processor.Input('1'); //return true and iterates to next step
    char[] validInput = processor.GetValidInput(); //returns new char[]{'2','3'}

    bool step2 = processor.Input('4'); //return false because on step2 (2|3) is accepted
}

: , DFA/NFA.
https://github.com/moodmosaic/Fare , , , . , , . BasicOperation.Run( a, s) , IsMatch .

Regex
lib , . , , . - , . , . DFA/NFA, , , , .

+1

. , , , .

.

  • \
  • \d: |\d\d
  • \d\d: |\d:\d
  • \d\d:\d |\d:\d\d
  • \d\d:\d\d

, ( )

^(\d|\d:|\d\d|\d\d:|\d:\d|\d\d:\d|\d:\d\d|\d\d:\d\d)$

.

  • \d\d?
  • \d\d:
  • \d\d:\d
  • \d\d:\d\d

, .

^(\d\d?|\d\d?:|\d\d?:\d|\d\d?:\d\d)$
+1

Source: https://habr.com/ru/post/1649535/


All Articles