-10

From the following String I want to match charaters between { }

I tested {"chkin.*"} but in code it is not working

    String re = "\\{\"chkin.*}";
    pattern = Pattern.compile(re);
    matcher = pattern.matcher(document.html());

document = {"chkin":"2018/1/18","chkout":"2018/1/19","adults":"2","children":"1","childAge":"2","brandId":"2199","packageType":null,"countryId":0,"isVip":false,"swpToggleOn":null,"cacheId":null,"tla":"NGO","stayLength":null,"daysInFuture":null,"ticketedTravelers":null,"evalMODExp":true,"partnerName":"","partnerPrice":"0.0","partnerCurrency":"","partnerTimestamp":"0"},"taapPackageRateEnabled":false,"loyaltyData":{"rewardsAmount":0,"formattedRewardsAmount":null,"rewardsDollarValue":null,"redemptionFloorAmount":0,"expediaPlusBranded":"Expedia+","expediaPointsBranded":"Expedia+ points","awardType":"points","hasLoyaltyEarnings":true,"hasGPSLoyaltyEarnings":false,"tier":null,"swpToggleDefaultState":true,"canUserBurn":false,"displaySwpToggle":null},"displayQualifyingNights":false,"showExcludeTaxMessage":true,"showNoRefundIcon":false,"drrMessageForGPSEnabled":false,"omnitureData":

This document is a very large html file I am showing only part of it.

Fixed :

Problem was at document.html() part of code. Actually document was a huge html file that I have shown just a part. So Intellij Idea was complaining that very large String constant inside matcher. So I divided large html file into chunks and then regex suggested by @Bohemian worked perfectly fine. Thanks

Avinash
  • 2,093
  • 4
  • 28
  • 41

1 Answers1

0

Your regex is greedy; it will match everything up to the last } it can find.

Try this:

String re = "\\{\"chkin.*?\\}";

Adding the ? makes it reluctant, which matches as little as possible.

Bohemian
  • 412,405
  • 93
  • 575
  • 722
  • Exception in thread "main" java.util.regex.PatternSyntaxException: Illegal repetition near index 1 \\{\"chkin.*?} ^ – Avinash Dec 14 '17 at 02:25
  • @Avi try the edited version – Bohemian Dec 14 '17 at 02:25
  • Exception in thread "main" java.lang.IllegalStateException: No match found at java.util.regex.Matcher.group(Matcher.java:536) at java.util.regex.Matcher.group(Matcher.java:496) – Avinash Dec 14 '17 at 02:27
  • 2
    @Avi I can't reproduce your error, this regex compiles fine https://ideone.com/xmx3r7 (even without escaped `}` since it is not special unless it closes unescaped `{`). Post your *real* code. – Pshemo Dec 14 '17 at 02:27
  • I think your regex is fine may be some problem with `document.html() ` part of my code. – Avinash Dec 14 '17 at 02:37
  • As I mentioned before problem was at `document.html()` part of code. Actually `document` was a huge `html` file that I have shown just a part. So `Intellij Idea` was complaining that `very large String constant inside matcher`. So I divided large `html` file into chunks and then above `regex` suggested by @Bohemian worked perfectly fine. Thanks. – Avinash Dec 15 '17 at 01:56