File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to use grep in regular expression java

 
deepika deepi
Ranch Hand
Posts: 199
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 47300
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 199
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1557
5
Eclipse IDE Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Tim Moores
Bartender
Posts: 2502
14
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 199
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Bartender
Posts: 2502
14
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 9501
50
Eclipse IDE Hibernate Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Campbell Ritchie
Sheriff
Pie
Posts: 47300
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 199
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 199
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 9501
50
Eclipse IDE Hibernate Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20836
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
deepika deepi
Ranch Hand
Posts: 199
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jtidy is used to show praser for html/xml tag ?
 
Winston Gutkowski
Bartender
Pie
Posts: 9501
50
Eclipse IDE Hibernate Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have a look at grep4j http://code.google.com/p/grep4j/
 
Campbell Ritchie
Sheriff
Pie
Posts: 47300
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
marco castigliego, welcome to the Ranch
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic