• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Regex Pattern for fetching a particular group

 
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Can someone help me in writting pattern to fetch 20ABC.

If I use Pattern1 for fetching the values,then even a part of date is captured. I want 20ABC TIRE DEALERS /S/ to be captured in a group so that I can split and retrive 20ABC

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class PatternMatching {

public static void main (String args[]) throws Exception {

List<String> arrItems=new ArrayList<String>();
arrItems.add("DIRECTORY NAME STATE BUSINESS TELEPHONE NO.");
arrItems.add("03/03/2005 ABCD ALPHA 16 25.25 _____");
arrItems.add("02/27/2006 SBR QWERTY OIL CHANGE & LUBRICATION SERVICE 20 22.95 _____");
arrItems.add("01/19/1999 SRQL QWERTY REPAIR & SERVICE 1 FREE _____");
arrItems.add("02/27/2006 20ABC TIRE DEALERS /S/ 19 111.60 _____");

for (int i = 0; i < arrItems.size(); i++) {
if(arrItems.get(i).contains("DIRECTORY"))
{
for (int j=i; j < arrItems.size(); j++) {

String str = arrItems.get(j);

Pattern pattern = Pattern.compile("(\\d{1,2}\\/\\d{1,2}\\/\\d{4})\\s*(([\\/\\&a-zA-Z]*\\s*)+)\\s*(\\d*)\\s*((\\d*\\.\\d*)|(\\w*))");
Pattern pattern1 = Pattern.compile("(\\d{1,2}\\/\\d{1,2}\\/\\d{4})\\s*(([\\/\\&a-zA-Z0-9]*\\s*)+)\\s*(\\d*)\\s*((\\d*\\.\\d*)|(\\w*))");

Matcher matcher = pattern.matcher(str);

if (matcher.find()) {
System.out.println("***************");
System.out.println("Group -->"+matcher.group());
System.out.println("In Date -->"+matcher.group(1));

String[] splitID = matcher.group(2).split("\\s+");
System.out.println("Item Name -->"+splitID[0]);
final List<String> list = new ArrayList<String>();
Collections.addAll(list, splitID);
list.remove(splitID[0]);
splitID = list.toArray(new String[list.size()]);
String s1 = Arrays.asList(splitID).toString();
System.out.println("Heading Name -->"+s1.substring(1, s1.length()-1).replaceAll(",", ""));
System.out.println("***************");
}

}

}}

}
}
 
Ranch Hand
Posts: 729
7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This too has not much to do with ws and will probably be moved to an more appropriate forum sooner or later.

In the meantime, I would say pattern2 captures the needed feature of having alpha-numeric "word" 20ABC absolutely required to start with. Then I would enforce the cardinality from * to + for the rest.

It probably should do what you need.
 
Dhivya Krishnan
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you so much. It works..
 
reply
    Bookmark Topic Watch Topic
  • New Topic