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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T06:38:36+00:00 2026-05-29T06:38:36+00:00

I have an input string in the following format String input = 00IG356001110002005064007000000; Characters

  • 0

I have an input string in the following format

String input = "00IG356001110002005064007000000";

Characters 3-7 is the code.

Characters 8-12 is the amount.

Based on the code in the input string (IG356 in the sample input string), i need to capture the amount(00111 in the sample).
The value in the amount (characters 8-12) should be picked up only for specific codes and the logic is detailed below.

  1. The code should not be SG356. If it is SG356, not a match and exit.
  2. a. If the code is not SG356, check if the codes are IG902 or SG350, in this case capture the amount(00111)

    else

    b. Check for the 3 numbers in the code (characters 5-7, 356 in this sample). If they are 200,201,356,370. go ahead and capture the amount

I am using the regular expression shown below:
Using positive lookahead and if then else construct.

String regex= ".{2}(?!SG356)((?=IG902|SG350).{5}(.{5}).+|.{2}(?=200|201|356|370).{3}(.{5}).+)";

The regular expression works fine if the code in the input string is IG902 or SG350 (when the ‘if’ part of the regex is getting matched). but if the ‘else’ is getting matched, i am unable to capture the amount.

This regular expression is working fine while just checking for a match.

.{2}(?!SG356)((?=IG902|SG350).+|.{2}(?=200|201|356|370).+) 

The problem is only while capturing the group.
I am running this in Java. Any help would be greatly appreciated.

The java code i am using is :

public String getTsqlSum(String input, String regex){
     String value = null;
     Matcher m = Pattern.compile(regex).matcher(input);
     System.out.println("Group Count: " + m.groupCount());
     if (m.matches()) {
    for (int i=0;i<m.groupCount();i++){
        System.out.println("For i: " + i +" Value: " + m.group(i));
         }
     }
     return value;
}

public void forumTest(){
    //String input = "00IG902001110002005064007000000";
         String input = "00IG356001110002005064007000000";
    String regex= ".{2}(?!SG356)(?:(?=IG902|SG350).{5}|.{2}(?=200|201|356|370).{3})(.{5}).+";
    System.out.println(match(input, regex));
    String match = getTsqlSum(input, regex);
    System.out.println("Match: " + match);
}
  • 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-29T06:38:37+00:00Added an answer on May 29, 2026 at 6:38 am

    The regular expression works fine if the code in the input string is IG902 or SG350 (when the ‘if’ part of the regex is getting matched). but if the ‘else’ is getting matched, i am unable to capture the amount.

    You are not unable to capture the amount, the expression is working fine. But if you are in the second part of the alternation (This is not a regex if-then-else) then your result is in a different capturing group. You will find it in the capturing group 3 and not in the second one like when you are matching in the first part of the alternation.

    String regex= ".{2}(?!SG356)((?=IG902|SG350).{5}(.{5}).+|.{2}(?=200|201|356|370).{3}(.{5}).+)";
            Group number        1                   2                                   3
    

    In a regular expression the capturing groups are numbered by their opening brackets and this continues also in an alternation. In Perl there would be a construct that gives the capturing groups of an alternation the same number, but I think thats the only flavour that is able to do this.

    In Java you need to check after the expression in which group you have the result.

    See my answer here, similar topic

    You can change your regex and make the alternation before the capturing group

    try this

    .{2}(?!SG356)(?:(?=IG902|SG350).{5}|.{2}(?=200|201|356|370).{3})(.{5}).+
    

    You will find your result in both cases in the group 1. (I made the first one a non capturing group using the ?:)

    Update after the source was added

    Your loop is wrong, that means the groups are starting at 1, if you want the content of group one, you have to use m.group(1).

    In group m.group(0) you will find the whole matched string.

    Try this

    for (int i=1;i<=m.groupCount();i++){
        System.out.println("For i: " + i +" Value: " + m.group(i));
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi I have the following date as String format. Input 2010-04-20 05:34:58.0 Output I
I have an input string in the following format: 12.34 When I call this
I have an input string that's generated as in following example: string.Format(Document {0}, was
I have the following C# which simply replaces parts of the input string that
Every time I run the following code, it keeps on saying Input string was
I have the following JSON string coming from external input source: {value: "82363549923gnyh49c9djl239pjm01223", id:
I have a string in the following format. 07/06/2011 5:06 mm/dd/yyyy hh:mm am using
I have an input string which is a directory address: Example: ProgramFiles/Micro/Telephone And I
I have a string input = "maybe (this is | that was) some ((nice
I have a string input that i do not know whether or not is

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.