Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6971873
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T16:53:43+00:00 2026-05-27T16:53:43+00:00

I have an Android application in which I’m trying to parse a small sized

  • 0

I have an Android application in which I’m trying to parse a small sized XML response coming from server. I have simply just used a regex for doing that, but now I’m facing a problem dealing with optional XML-tags that may or may not be present in the response’s payload. My regex is declared as

final String regex = "^(?=.*<x>(.+)</x>)(?=.*<r>(.+)</r>)?(?=.*<e>(.+)</e>)" +
            "(?=.*<h>(.+)</h>)(?=.*<y>(.+)</y>)(?=.*<n>(.+)</n>).*$";

Note the question mark after the second look ahead group to have the < r > tag be optional. It is causing me an exception when compiling with Pattern.compile:

Pattern p = Pattern.compile(regex);
...
12-19 10:19:21.257: E/AndroidRuntime(2342): Caused by: java.util.regex.PatternSyntaxException: Syntax error U_REGEX_RULE_SYNTAX near index 36:
12-19 10:19:21.257: E/AndroidRuntime(2342): ^(?=.*<x>(.+)</x>)(?=.*<r>(.+)</r>)?(?=.*<e>(.+)</e>)(?=.*<h>(.+)</h>)(?=.*<y>(.+)</y>)(?=.*<n>(.+)</n>).*$
12-19 10:19:21.257: E/AndroidRuntime(2342):                                     ^
12-19 10:19:21.257: E/AndroidRuntime(2342):     at java.util.regex.Pattern.compileImpl(Native Method)
12-19 10:19:21.257: E/AndroidRuntime(2342):     at java.util.regex.Pattern.compile(Pattern.java:400)
12-19 10:19:21.257: E/AndroidRuntime(2342):     at java.util.regex.Pattern.<init>(Pattern.java:383)
12-19 10:19:21.257: E/AndroidRuntime(2342):     at java.util.regex.Pattern.compile(Pattern.java:374)

Without the ?-quantifier it compiles just fine.

I tried also using the same pattern on Windows with desktop java (jdk 1.6.0_24) and it worked as it should (and even parsed my test payload correctly). Are there some known limitations with the native regex implementation on Android or am I just missing something here? I already read the Android documentation regarding Pattern but to no avail. Any help would be appreciated, I wouldn’t like to resort to having to use a dedicated XML parser for something as simple as this.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T16:53:44+00:00Added an answer on May 27, 2026 at 4:53 pm

    You usually can’t make a lookahead optional by appending a ?. This is a syntax error in some regex dialects (because a lookahead assertion doesn’t match anything that could be quantified by the ?).

    But you can wrap the lookahead in an optional non-capturing group:

    final String regex = "^(?=.*<x>(.+)</x>)(?:(?=.*<r>(.+)</r>))?(?=.*<e>(.+)</e>)" +
                "(?=.*<h>(.+)</h>)(?=.*<y>(.+)</y>)(?=.*<n>(.+)</n>).*$";
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an Android application which grabs some data from an external XML source.
I have an Android application which retrieves from a external server, a name and
I have an Android application from which I want to upload some data to
I am developing an android application in which i have to parse the url
I have a webservice in android application which i have to call.The response of
I have an Android application which downloads around 100 small images and and 30
I have an Android application which retrieves information from the web using an AsyncTask
i have created an android application which takes the input qrcode image from it
I have an Android application in which I have my preferences in an XML
I have an interesting problem being reported to me from an android application I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.