Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

FIND REGEX

achraf_smaali
Explorer
0 Likes
1,443

Hello Abapers,

I am having an excpetion triggered due to this expression

      FIND REGEX '\A\s*"([^:]*)"\s*:' IN SECTION OFFSET l_offset OF json
                        MATCH OFFSET l_offset MATCH LENGTH l_len
                        SUBMATCHES l_submatch.
      IF sy-subrc NE 0.
        RAISE EXCEPTION TYPE zcx_mdp_json_invalid.
      ENDIF.

The Json variable value is :

{"evenement":{"Id":"ERROR : No result found for Id :0251407282","Partner":null,"Pod":null,"Contract":null,"Services":null,"Remises":null}}

I don't know what's wrong exactly, I tried to look for documentation but I couldn't find something useful.

Any tips or advice please?

Best regards,

Hello Abapers,

I am having an excpetion triggered due to this expression

      FIND REGEX '\A\s*"([^:]*)"\s*:' IN SECTION OFFSET l_offset OF json
                        MATCH OFFSET l_offset MATCH LENGTH l_len
                        SUBMATCHES l_submatch.
      IF sy-subrc NE 0.
        RAISE EXCEPTION TYPE zcx_mdp_json_invalid.
      ENDIF.

The Json variable value is :

{"evenement":{"Id":"ERROR : No result found for Id :0251407282","Partner":null,"Pod":null,"Contract":null,"Services":null,"Remises":null}}

I don't know what's wrong exactly, I tried to look for documentation but I couldn't find something useful.

Any tips or advice please?

Best regards,

3 REPLIES 3
Read only

felipe_dimmu23
Participant
1,279

My best guess is that the regex is wrong, have you tried to validate using https://regex101.com/

or even report demo_regex (with extended examples on)?

What are you trying to match in the json?

Read only

Sandra_Rossi
Active Contributor
1,279

Your code triggers an exception, but your question is about why the regex doesn't find the value (i.e. why SY-SUBRC = 0). Quickly looking at it, your regex seems wrong:

"([^:]*)"

it should be:

"([^"]*)"

In fact, if you are trying to do something generic, it's not even handle all the cases, but well that's not the question... Even regex is not recommended for parsing JSON.

(NB: \A is correct only if you play correctly with L_OFFSET, that you should change to go after curly brackets and comma)

Read only

RaymondGiuseppi
Active Contributor
1,279

Did you also look at a class such as /UI2/CL_JSON and/or a statement such CALL TRANSFORMATION, there are some blogs in the forum, for a more robust development than cutting the text manually.