Highlighted
Trusted Contributor.
Trusted Contributor.
801 views

Issue in capturing correlation value using regular expression

Jump to solution

Hi

Iam trying to capture  value= 40 in the below responce of a request. Here name and Code gets repeated, so need to ignore the first group. Can any one help me to achieve this.

[{"ProfileId":"123456789","name":"Test1","Code":"12345","Field":[{"FieldId":"29","FieldName":"Sample","PackageName":"Samplesize","sizeValue":"{\\r\\n  \\"Symbol\\": \\"
g\\",\\r\\n  \\"Value\\": \\"40\\",\\r\\n  \\"Type\\": 0,\\r\\n  \\

Iam getting invalid regular expression in the output

     web_reg_save_param_regexp("ParamName=Correlatedvalue",
        "RegExp=\"ProfileId\":\"123456789\",\"(.*?)",\"Field\":[{\"FieldId\":\"29\",\"FieldName":\"Sample\",\"PackageName\":\"Samplesize\",\"sizeValue\":\"{\\r\\n  \\\"Symbol\\\": \\\"g\\\",\\r\\n  \\\"Value\\\": \\\"(.*?)\\\",\\r\\n  \\\"Type\\\"",
        SEARCH_FILTERS,
        "Scope=Body",
        "IgnoreRedirections=No",
        "Group=2",
        LAST)     

Thanks in advance.

KVS Kumar 

0 Likes
1 Solution

Accepted Solutions
Highlighted
Acclaimed Contributor.. Acclaimed Contributor..
Acclaimed Contributor..

Re: Issue in capturing correlation value using regular expression

Jump to solution

Hi KVS Kumar,

Good catch! I think that LR is reporting the lines with double backslash '\\n' and tripple '\\\"', so in 'C-string' format. I've seen it before, but was not allert enough.

But figuring this out was a good practice. Without help of a good website this kind of expressions are very error prone and hard to maintain.

Success

Signature:
Reward community members who take time to respond and help.

View solution in original post

0 Likes
3 Replies
Highlighted
Acclaimed Contributor.. Acclaimed Contributor..
Acclaimed Contributor..

Re: Issue in capturing correlation value using regular expression

Jump to solution

Writing regular expressions on JSON data is cumersome.

You need to escape your literal characters at two levels:

1: at regular expression level

2: at C-code level.

For 1 you need to escape literal characters: [ ] \ . * ? + { } (see documentation)

For level 2 you need to escape: " \

So your expression (remove new lines) become at level 1:

\[{"ProfileId":"123456789","(.*?)"
,"Field":\[{"FieldId":"29","FieldName":"Sample"
,"PackageName":"Samplesize"
,"sizeValue":"{\\\\r\\\\n  \\\\"Symbol\\\\": \\\\"g\\\\"
,\\\\r\\\\n  \\\\"Value\\\\": \\\\"(.*?)\\\\"
,\\\\r\\\\n  \\\\"Type\\\\": 0,\\\\r\\\\n

To make it a C-string:

"\\[\\{\"ProfileId\":\"123456789\",\"(.*?)\"
,\"Field\":\\[\\{\"FieldId\":\"29\",\"FieldName\":\"Sample\"
,\"PackageName\":\"Samplesize\"
,\"sizeValue\":\"\\{\\\\\\\\r\\\\\\\\n  \\\\\\\\\"Symbol\\\\\\\\\": \\\\\\\\\"g\\\\\\\\\"
,\\\\\\\\r\\\\\\\\n  \\\\\\\\\"Value\\\\\\\\\": \\\\\\\\\"(.*?)\\\\\\\\\"
,\\\\\\\\r\\\\\\\\n  \\\\\\\\\"Type\\\\\\\\\": 0,\\\\\\\\r\\\\\\\\n"

A very handy site is regex101, you can build your regular expression and can translate it into a C string (take 'code generator' and language Java).

But still your question is not clear and I'm not sure if you approach will work. Why not using lr_eval_json() and lr_json_*() functions.

Signature:
Reward community members who take time to respond and help.
0 Likes
Highlighted
Trusted Contributor.
Trusted Contributor.

Re: Issue in capturing correlation value using regular expression

Jump to solution

Thank you JHF Remmelzwaal for the reply. I have tried with the given format but still it is showing invalid regular expression. However i tried with 4 backslashes for \r and \n and 5 backslashes for \\\\\"Value\\\\\": and got the expected result.

\"ProfileId":\"123456789\",\"(.*?)\"
,\"Field\":\\[{\"FieldId":\"29\",\"FieldName\":\"Sample\"
,\"PackageName\":\"Samplesize\"
,\"sizeValue\":\"{\\\\r\\\\n  \\\\\"Symbol\\\\\": \\\\\"g\\\\\"
,\\\\r\\\\n  \\\\\"Value\\\\\": \\\\\"(.*?)\\\\\"
,\\\\r\\\\n  \\\\\"Type\\\\\": 0,\\\\r\\\\n

I have also tried with online regex tester and it is very helpful for verifying the expression.

Thank you very much JHF Remmelzwaal.

 

0 Likes
Highlighted
Acclaimed Contributor.. Acclaimed Contributor..
Acclaimed Contributor..

Re: Issue in capturing correlation value using regular expression

Jump to solution

Hi KVS Kumar,

Good catch! I think that LR is reporting the lines with double backslash '\\n' and tripple '\\\"', so in 'C-string' format. I've seen it before, but was not allert enough.

But figuring this out was a good practice. Without help of a good website this kind of expressions are very error prone and hard to maintain.

Success

Signature:
Reward community members who take time to respond and help.

View solution in original post

0 Likes
The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.