FHIR Chat · Regex question · hapi

Stream: hapi

Topic: Regex question


view this post on Zulip Grahame Grieve (Mar 10 2022 at 00:04):

hi Java gurus. Consider this JUnit test:

    Assertions.assertTrue("http://fhir.org/guides/cqf/common/Library/FHIR-ModelInfo|4.0.1".matches("Library"));

view this post on Zulip Grahame Grieve (Mar 10 2022 at 00:04):

it fails. But according to https://regex101.com, that should pass (java 8 mode).

view this post on Zulip Grahame Grieve (Mar 10 2022 at 00:04):

What's going on here?

view this post on Zulip Grahame Grieve (Mar 10 2022 at 00:11):

that's because matches must match the whole string, i presume (you can tell I'm expert at regular expressions).

If I need to just search for a match in the string (not the whole string), I can prepend and append .*

is that always safe? is there a better way?

view this post on Zulip Patrick Werner (Mar 10 2022 at 08:28):

If you are just searching a String in a String String.contains() would be the easiest. For matches ().*Library.*is fine if you don't expect new line characters.

view this post on Zulip Gino Canessa (Mar 10 2022 at 15:33):

Note that would also match on strings like http://example.org/Library.html, if that is matters (don't know the context of the match).


Last updated: Apr 12 2022 at 19:14 UTC