aspose file tools*
The moose likes Beginning Java and the fly likes how to use grep in regular expression java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "how to use grep in regular expression java" Watch "how to use grep in regular expression java" New topic
Author

how to use grep in regular expression java

deepika deepi
Ranch Hand

Joined: Jan 23, 2012
Posts: 199
hi experts,

i have an text like this

<name>Java</name> has methods
<name>JavaProgramming</name> have methods
<name>java</name> has methods

and my expected output is

<name>Java</name> has methods

and print Java stripping has methods using grep after matching the regular expression


the code which i used for regular expression is as follows

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Matching {

// public static final Pattern commonPattern = Pattern.compile("(Java|JavaProgramming) (has|have) methods");

//public enum Names {Java,JavaProgramming}

public static void main(String[] args) {

// String name = "Java|java|JavaProgramming";
String value = "have|has";
String name = "<name>.*.</name>";
//Pattern pattern = Pattern.compile("("+name+").*.(</name>).*.("+value+") methods");
Pattern pattern = Pattern.compile("(<name>).*.(</name>).*.("+value+") methods");
// Pattern pattern = Pattern.compile("("+name+") ("+value+") methods");
String text="<name>Java</name> has methods\n"+ "<name>JavaProgramming</name> have methods\n"+"<name>java</name> has methods";

System.out.println("Words from the Text is:-");
System.out.println(text);
System.out.println("========================");
Matcher m = pattern.matcher(text);

System.out.println("Finded words from the Text is:-");
while (m.find())
System.out.println(m.group());
}}

how to use grep in regular expression in java and get the output ??

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39791
    
  28
Don’t call us experts; some people might think it rude.
I doubt whether you can use regular expressions for parsing XML, which is what you appear to be doing. Seek an XML parser.
deepika deepi
Ranch Hand

Joined: Jan 23, 2012
Posts: 199
ok fine. if there is no xml praser in the input i have an plain text like this

Java has methods
JavaProgramming have methods
java has methods

and i want only Java has methods and next get output Java on the output window stripping the next two sentences using grep command in java.is it possible to use grep and get the output and can any one tell me how to use grep in java?
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1509
    
    5

vijayalakshmi deepika wrote:and i want only Java has methods and next get output Java on the output window stripping the next two sentences using grep command in java

Can you please clarify about it? I didn't understand what is exact output you are seeking.
vijayalakshmi deepika wrote:how to use grep in java?

There's nothing special about grep in Java. All you need is to understand two things:
1) How to use grep command
2) How to run command and fetch output from Java.

From Java code perspective, it doesn't matter which command you are running. There is same API to run and get output of any command - ls, grep, cat etc.

I hope this helps.


Regards,
Anayonkar Shivalkar (SCJP, SCWCD, OCMJD, OCEEJBD)
Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2408
The Jakarta ORO library has several utilities that may be helpful with tasks like this.

And Campbell's right, regexps can't be used to deal with XML in general, they're not powerful enough.
deepika deepi
Ranch Hand

Joined: Jan 23, 2012
Posts: 199
understood.but can you tell me how to use grep command, run the command and fetch the output in java? or any tutorials available where i can understand it easily.i have searched in net andunable to understand how to use grep command.so i put my queries here for getting the output?
Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2408
how to use grep no good? You can use Runtime.exec or the ProcessBuilder class to run grep from within Java code, assuming that the underlying OS has grep available. On Windows you can install UnxUtils which includes grep.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8196
    
  23

vijayalakshmi deepika wrote:understood.but can you tell me how to use grep command, run the command and fetch the output in java? or any tutorials available where i can understand it easily.i have searched in net andunable to understand how to use grep command.so i put my queries here for getting the output?

You can run the grep command, but it seems like a redundant, slow and error-prone way of doing things to me (you might want to look at this article), since grep stands for 'general regular expression parser', and Java already handles regular expressions natively in its Strings.

And BTW, Campbell and Tim are absolutely right: if this is HTML or XML, you don't want to be using regexes at all.

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39791
    
  28
Tim Moores wrote: . . . And Campbell's right, regexps can't be used to deal with XML in general, they're not powerful enough.
XML is probably a context-sensitive grammar, because <foo> must be followed by </foo>. Regular expressions can only parse something in a regular grammar.
deepika deepi
Ranch Hand

Joined: Jan 23, 2012
Posts: 199
As your telling the regular expression is not necessary for XML/HTML tags.but instead of tags like that i use open bracket or parenthesis

(name) Java (/name) has methods or {name}Java{/name} has methods

can i use regular expression and get that Word inside that parenthesis or bracket and get the output.IS that possible?

i want to extract particular word in this parenthesis or bracket and get the output using regular expression ? is this possible ?
deepika deepi
Ranch Hand

Joined: Jan 23, 2012
Posts: 199
i thank you for telling how to run grep. thanks now i understood the how to see that word and get the output using Grep
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8196
    
  23

deepika deepi wrote:As your telling the regular expression is not necessary for XML/HTML tags.but instead of tags like that i use open bracket or parenthesis

No, what we're trying to say is that regexes (or grep) are inappropriate for XML/HTML. There are however, many parsers out there which were specifically designed for either (or both). My suggestion would be JTidy, but there are loads of others.

Winston
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18978
    
  40

deepika deepi wrote:i thank you for telling how to run grep. thanks now i understood the how to see that word and get the output using Grep


Not sure of the point to this -- the java regular expression engine can do everything grep can do, and more. It is not very efficient to start another process to do something that can be done by the application internally.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
deepika deepi
Ranch Hand

Joined: Jan 23, 2012
Posts: 199
Jtidy is used to show praser for html/xml tag ?
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8196
    
  23

deepika deepi wrote:Jtidy is used to show praser for html/xml tag ?

Yes, and it's just one of many.

Winston
marco castigliego
Greenhorn

Joined: Jul 04, 2012
Posts: 4
Have a look at grep4j http://code.google.com/p/grep4j/
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39791
    
  28
marco castigliego, welcome to the Ranch
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to use grep in regular expression java