Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Java > Java and JDK > Java Basics
|
Java Basics General beginning Java language questions that don't fit in one of the more specific forums. Please specify what version.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Java Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 8th, 2007, 05:33 AM
Registered User
 
Join Date: May 2007
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default RegExp matching substring

RegExp is new to me!
I have a string which contains few commas that the number/place of the commas are not predictable.
I need the substring that begins from the 5th comma from the end, and forward.

example:
full string:
  OU=Praklitim,OU=Sanegoria,OU=JR-Maatz,OU=OUs,DC=justice,DC=gov,DC=il
substring:
  OU=JR-Maatz,OU=OUs,DC=justice,DC=gov,DC=il

Thank you,
Maya.
 
Old May 8th, 2007, 07:56 AM
Authorized User
 
Join Date: Oct 2006
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Try the code below. Hope that's what you need

----------------------------------------------


    String str =
        "OU=Praklitim,OU=Sanegoria,OU=JR-Maatz,OU=OUs,DC=justice,DC=gov,DC=il";
    String result = "";
    String[] stringMassive = str.split(",");
    for (int i = stringMassive.length - 5; i < stringMassive.length; i++) {
      result = result.concat(stringMassive[i]);
      if (i != stringMassive.length - 1) result = result.concat(",");

    }
    System.out.println(result);

----------------------------------------------

Best,
Anna
 
Old May 9th, 2007, 12:41 AM
Registered User
 
Join Date: May 2007
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you Anna,
But
I was looking for a regular Expression code. There are a lot of ways to Solve the problem and one of them is yours. Are you formilier with RE?
Maya.

 
Old May 11th, 2007, 04:48 AM
Authorized User
 
Join Date: Oct 2006
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
Default

http://www.regular-expressions.info/java.html

Split() method in String class is one of "Quick Regex Methods of The String Class".

And you can get the same string massives via regular expression code:
----------------------------------

Pattern myPattern = Pattern.compile("OU=Praklitim,OU=Sanegoria,OU=JR-Maatz,OU=OUs,DC=justice,DC=gov,DC=il");
String[] stringMassive = (myPattern.pattern()).split(",");

-----------------------------------
 
Old May 11th, 2007, 08:11 AM
Authorized User
 
Join Date: Oct 2006
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Oh, guess I understand you finally. Is it what you need?

---------------------------------------------------


   String input = "OU=Praklitim,OU=Sanegoria,OU=JR-Maatz,OU=OUs,DC=justice,DC=gov,DC=il";
     int i = 0; int j = 0;
     Pattern p = Pattern.compile(",");
     Matcher m = p.matcher(input);
     while (m.find()) {
     i++;

    }
    while (m.find()) {
      if (j == i - 5) System.out.println(input.subSequence(m.start() + 1, input.length()));
      j++;
    }

----------------------------------------------

Best,
Anna

P.S. But IMHO String methods are more simple to use and to understand ;)





Similar Threads
Thread Thread Starter Forum Replies Last Post
RegExp Problem Abbas Classic ASP Professional 0 August 2nd, 2006 10:48 AM
RegExp crmpicco Javascript How-To 0 July 7th, 2005 05:59 AM
RegExp crmpicco Classic ASP Basics 0 June 6th, 2005 05:54 AM
RegExp Nitin_sharma Javascript 2 November 30th, 2004 07:21 AM
regexp problem godhsf80 PHP How-To 2 March 10th, 2004 04:54 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.