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 7799389
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T00:17:34+00:00 2026-06-02T00:17:34+00:00

I am new to Android coding, but I have experience with Perl regex. I

  • 0

I am new to Android coding, but I have experience with Perl regex.
I need to match a list of 0 or more identifiers with a regular expression like:

^\s*((\w\d\d\d)(\s+$2)*)?$

Note the $2 refers to the previous matched group (\w\d\d\d)
For android code it would look like:

Pattern.compile("^\\s*((\\w\\d\\d\\d)(\\s+\$2)*)?$")

Eclipse compiler does not compile the \$2, I tried also \2, which compiles but tries to match a literal number 2.

The brute force solution would be to repeat the identifier pattern:

Pattern.compile("^\\s*((\\w\\d\\d\\d)(\\s+(\\w\\d\\d\\d))*)?$")

It works, but it has the following disadvantages:
* It is easy to make a syntax error in either repetition
* as the identifier gets more complicated the string gets big
* it is not elegant
* gets much more complicated if you need to refer not to one but several previous matches

Is there a way in Java to refer to previous matched groups within the regex?

  • 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-06-02T00:17:36+00:00Added an answer on June 2, 2026 at 12:17 am

    I am sorry about the confusion, my confusion.
    The regex:

    Pattern.compile("^\\s*((\\w\\d\\d\\d)(\\s+\\$2)*)?$")
    

    will match something like “A12 A12”, therefore matching the previous match. (I just tried it on Eclipse and followed it with the debugger)

    What I wanted is a way to write a short regex for a string like “A12 B35 C36 A011”
    In perl you can use variables a part of the pattern, therefore it can be done in perl:

    $a='\w\d\d\d';
    $mystring =~ /^\s*(($a(\s+$a)*)?$/;
    

    Short and simple. Therefore I assumed in java it can also be done by concatenating strings (I just tried it and it works)

    String id="\\w\\d\\d\\d";
    Pattern.compile("^\\s*(" + id + "(\\s+" + id + ")*)?$");
    

    It is not elegant, but it does the job.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Quite new to coding for android but this issue has me tearing my hair
I am quite new to Java and am currently coding an Android application. In
i am creating a new android application.i am using the table layout. I have
I am new to android. I have a loginActivity which validates a user number
I am new to android coding and was making a project with ListView. I
I am trying to Set Location but getting NullPointerException , i have Provided android.permission.ACCESS_COARSE_LOCATION
I'm fairly new to Java & Android, so I have little idea what I
I have an EditText view in my Android app. I need inner links in
The new Android 2.1 SDK (version 7) has a new class called SignalStrength: http://developer.android.com/reference/android/telephony/SignalStrength.html
I just installed the new Android SDK and the ADT 17. After the Installation

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.