Welcome to Dream.In.Code
Become a Java Expert!

Join 150,184 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 2,093 people online right now. Registration is fast and FREE... Join Now!




Regular Expressions: 2 conditions?

 
Reply to this topicStart new topic

Regular Expressions: 2 conditions?

Synth
31 Oct, 2007 - 07:15 PM
Post #1

New D.I.C Head
*

Joined: 29 Aug, 2007
Posts: 44


My Contributions
ok so ive looked at the regular expressions like people told me and ive been wondering if it was possible to have the program check between two patterns at the same time. like instead of
CODE

String a = "1/2/3/4";

Pattern p = Pattern.compile("/");
String[] a = p.split(input);

System.out.println(a);

resulting in an out put of:
1
2
3
4


what i'm trying to do is have the program look between two sets of patterns so if the input was (1234)(4567) the program would pull out just the number between the parenthesis to put it in an array. is that possibleusing a combination of .split and Pattern or am i going in a completely wrong direction?
User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Regular Expressions: 2 Conditions?
31 Oct, 2007 - 08:20 PM
Post #2

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,660



Thanked: 313 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
In regular expressions you can have multiple pattern matches. Often times a regular expression will have multiple matches and programming languages often let you loop through the returned matches. For instance, applying a pattern of [0-9]{4}, which looks for a series of four numbers in a row, would return two matches for (1234)(4567). One matching 1234 and the other matching 4567. Often time regular expressions have a way of putting these matches in an array which then you could loop through with a for each loop or even a while loop would work.

Now you could use two separate calls to a regular expression which gives you results. One to split the string and another to pull out matches. Just make sure you take copies of the original string to work on so that your split doesn't destroy the string that you want to then pull matches out of.

Hopefully that answers your question. smile.gif

This post has been edited by Martyr2: 31 Oct, 2007 - 08:21 PM
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 04:03AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month