private JsonToken scanRegularExpression() {

        StringBuilder patternBuilder = new StringBuilder();
        StringBuilder optionsBuilder = new StringBuilder();
        RegularExpressionState state = RegularExpressionState.IN_PATTERN;
        while (true) {
            int c = buffer.read();
            switch (state) {
                case IN_PATTERN:
                    switch (c) {
                        case -1:
                            state = RegularExpressionState.INVALID;
                            break;
                        case '/':
                            state = RegularExpressionState.IN_OPTIONS;
                            break;
                        case '\\':
                            state = RegularExpressionState.IN_ESCAPE_SEQUENCE;
                            break;
                        default:
                            state = RegularExpressionState.IN_PATTERN;
                            break;
                    }
                    break;
                case IN_ESCAPE_SEQUENCE:
                    state = RegularExpressionState.IN_PATTERN;
                    break;
                case IN_OPTIONS:
                    switch (c) {
                        case 'i':
                        case 'm':
                        case 'x':
                        case 's':
                            state = RegularExpressionState.IN_OPTIONS;
                            break;
                        case ',':
                        case '}':
                        case ']':
                        case ')':
                        case -1:
                            state = RegularExpressionState.DONE;
                            break;
                        default:
                            if (Character.isWhitespace(c)) {
                                state = RegularExpressionState.DONE;
                            } else {
                                state = RegularExpressionState.INVALID;
                            }
                            break;
                    }
                    break;
                default:
                    break;
            }

            switch (state) {
                case DONE:
                    buffer.unread(c);
                    BsonRegularExpression regex
                        = new BsonRegularExpression(patternBuilder.toString(), optionsBuilder.toString());
                    return new JsonToken(JsonTokenType.REGULAR_EXPRESSION, regex);
                case INVALID:
                    throw new JsonParseException("Invalid JSON regular expression. Position: %d.", buffer.getPosition());
                default:
                    switch (state) {
                        case IN_OPTIONS:
                            if (c != '/') {
                                optionsBuilder.append((char) c);
                            }
                            break;
                        default:
                            patternBuilder.append((char) c);
                            break;
                    }
            }
        }
    }
private JsonToken scanRegularExpression ( ) { StringBuilder patternBuilder = new StringBuilder ( ) ; StringBuilder optionsBuilder = new StringBuilder ( ) ; RegularExpressionState state = RegularExpressionState . IN_PATTERN ; while ( true ) { int c = buffer . read ( ) ; switch ( state ) { case IN_PATTERN : switch ( c ) { case - 1 : state = RegularExpressionState . INVALID ; break ; case '/' : state = RegularExpressionState . IN_OPTIONS ; break ; case '\\' : state = RegularExpressionState . IN_ESCAPE_SEQUENCE ; break ; default : state = RegularExpressionState . IN_PATTERN ; break ; } break ; case IN_ESCAPE_SEQUENCE : state = RegularExpressionState . IN_PATTERN ; break ; case IN_OPTIONS : switch ( c ) { case 'i' : case 'm' : case 'x' : case 's' : state = RegularExpressionState . IN_OPTIONS ; break ; case ',' : case '}' : case ']' : case ')' : case - 1 : state = RegularExpressionState . DONE ; break ; default : if ( Character . isWhitespace ( c ) ) { state = RegularExpressionState . DONE ; } else { state = RegularExpressionState . INVALID ; } break ; } break ; default : break ; } switch ( state ) { case DONE : buffer . unread ( c ) ; BsonRegularExpression regex = new BsonRegularExpression ( patternBuilder . toString ( ) , optionsBuilder . toString ( ) ) ; return new JsonToken ( JsonTokenType . REGULAR_EXPRESSION , regex ) ; case INVALID : throw new JsonParseException ( "Invalid JSON regular expression. Position: %d." , buffer . getPosition ( ) ) ; default : switch ( state ) { case IN_OPTIONS : if ( c != '/' ) { optionsBuilder . append ( ( char ) c ) ; } break ; default : patternBuilder . append ( ( char ) c ) ; break ; } } } }
Reads {@code RegularExpressionToken} from source. The following variants of lexemes are possible:
<pre>
/pattern/
/\(pattern\)/
/pattern/ims
</pre>
Options can include 'i','m','x','s'

@return The regular expression token.
@throws JsonParseException if regular expression representation is not valid.