• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Tim Cooke
Sheriffs:
  • Rob Spoor
  • Liutauras Vilda
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
  • Piet Souris
Bartenders:
  • Stephan van Hulst

how to use grep in regular expression java

 
Ranch Hand
Posts: 199
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ??

 
Marshal
Posts: 79776
382
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Saloon Keeper
Posts: 7642
177
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Saloon Keeper
Posts: 7642
177
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79776
382
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author
Posts: 23956
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jtidy is used to show praser for html/xml tag ?
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have a look at grep4j http://code.google.com/p/grep4j/
 
Campbell Ritchie
Marshal
Posts: 79776
382
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
marco castigliego, welcome to the Ranch
 
I guess I've been abducted by space aliens. So unprofessional. They tried to probe me with this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic