Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improving pattern for regex capture and matches predicates #4

Closed
jcamiel opened this issue Aug 31, 2020 · 4 comments · Fixed by #461
Closed

Improving pattern for regex capture and matches predicates #4

jcamiel opened this issue Aug 31, 2020 · 4 comments · Fixed by #461
Labels
enhancement New feature or request
Milestone

Comments

@jcamiel
Copy link
Collaborator

jcamiel commented Aug 31, 2020

Currently, regex patterns are used in :

  • regex query
  • matches predicate
GET http://localhost:8000/assert-regex

HTTP/1.0 200
[Asserts]
regex "Hello ([0-9]+)!" not exists
regex "Hello ([a-zA-Z]+)!" equals "World"
body matches "Hello [a-zA-Z]+!"

Pattern in Hurl grammar use quoted-string: the problem is that backslash in quoted-string should be escaped. So to use special character in pattern (like \d, \s), backslash should be escaped, degrading the readability of the pattern:

regex "Hello (\\d)!" not exists

In Rust, Python, Kotlin, raw strings are used to improve the regex readability. In Rust, for instance:

use regex::Regex;
let re = Regex::new(r"^\d{4}-\d{2}-\d{2}$").unwrap();
assert!(re.is_match("2014-01-01"));

In the Hurl grammar, we already have raw string (```something\ntutu``` is literally the string "something\ntutu" without a newline between something and tutu). We could reuse this syntax when using regex query or matches predicates:

Before:

GET http://localhost:8000/assert-regex

HTTP/1.0 200
[Asserts]
regex "abc(\\d+)!" exists
jsonpath "$.date1" matches "\\d{4}-\\d{2}-\\d{2}"

After:

GET http://localhost:8000/assert-regex

HTTP/1.0 200
[Asserts]
regex ```abc(\d+)!``` exists
jsonpath "$.date1" matches ```\d{4}-\d{2}-\d{2}```

We could also introduce another syntax, like Python raw string and Rust raw string and keep ``` for body raw string:

GET http://localhost:8000/assert-regex

HTTP/1.0 200
[Asserts]
regex r"abc(\d+)!" exists
jsonpath "$.date1" matches r"\d{4}-\d{2}-\d{2}"

raw-string like r"abcd" could be used anywhere a quoted-string is used (not limited to regex capture and matches predicate)

@jcamiel jcamiel added the enhancement New feature or request label Aug 31, 2020
@fabricereix fabricereix removed the 1.5.0 label Jun 23, 2021
@humphd
Copy link

humphd commented Dec 5, 2021

I ran into this today, and was just coming to file a docs issue before seeing this already open. Here is what I hit:

10 | jsonpath "$.version" matches "\d+\.\d+\.\d+"
   |                                ^ The escaping sequence is not valid

It wasn't intuitive to me that I needed to escape my backslashes, especially since there aren't any examples of this in the docs (I looked at your tests to figure out what the problem was).

Could you support matches /\d+\.\d+\.\d+/ vs "\\d+\\.\\d+\\.\\d+" so that you can avoid this?

At the very least, more significant examples in the docs would help a lot.

@fabricereix
Copy link
Collaborator

Thanks for your suggestion.
I like the regex-specific type /.../ a lot.
It is quite standard and even used in javascript (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions).

It really improves the readability of the Hurl file.

@jcamiel
Copy link
Collaborator Author

jcamiel commented Dec 6, 2021

@fabricereix fabricereix added this to the 1.6.0 milestone Jan 27, 2022
@fabricereix
Copy link
Collaborator

fabricereix commented Jan 27, 2022

Regex Literals /.../ should probably not include any Hurl template.
Keeping the special characters '{' and '}' only for regex quantifiers.

We could maybe still use a string value for the match only if we need a template.

jsonpath "$.date" matches /\d{4}-\d{2}-\d{2}/              # using regex literal
jsonpath "$.date" matches "{{year}}-\\d{2}-\\d{2}"         # using string template

@fabricereix fabricereix linked a pull request Jan 29, 2022 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants