aspose file tools*
The moose likes Java in General and the fly likes In java  pattern matching how do declare string Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "In java  pattern matching how do declare string" Watch "In java  pattern matching how do declare string" New topic
Author

In java pattern matching how do declare string

deepika deepi
Ranch Hand

Joined: Jan 23, 2012
Posts: 199
hi,

i am having an doubt in pattern.compile while using that regular expression i have some doubt. my doubt is that in this
Pattern pattern = Pattern.compile("(Java|JavaProgramming) (has|have) methods");

i have (Java|JavaProgramming) and (has|have)

i need to declare that Java|JavaProgramming and has|have
if i give String deceleration like this

String regex = "Java|JavaProgramming";
String regex1 = "has|have";

i want this to have declared be common name instead if that pattern.compile

can any one tell me how to declare it in program some where and not in that pattern.compile??





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

public class Matching_Casesensitive {

public static void main(String[] args) {


Pattern pattern = Pattern.compile("(Java|JavaProgramming) (has|have) methods");
String text="Java has methods\n"
+ "JavaProgramming have methods\n";

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());
}}
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39393
    
  28
You should be careful where you post. This forum is for discussing the site, not specific questions about Java™. I shall take the liberty (Paul, I hope you don’t mind) of moving this thread somewhere more appropriate.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19718
    
  20

Please CarefullyChooseOneForum. The Ranch Office forum is for questions about the Ranch itself, not for technical questions. I'll move this thread to a more appropriate forum. And can you please UseCodeTags next time?

Edit: congrats Campbell, you beat me to it


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
deepika deepi
Ranch Hand

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

am i not clear with my questions?
Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4659
    
    5

I'm not sure I understand your question.
If you mean what I think you as asking, how about....

deepika deepi
Ranch Hand

Joined: Jan 23, 2012
Posts: 199
Dear experts,
but it gives the same names nothing is declared for that values
i need to declare like this

for example,

String name = "Java|JavaProgramming";
String value = " has|have";
and use that in that compile as

Pattern pattern = Pattern.compile("(name) (value) methods");

instead of
Pattern pattern = Pattern.compile("(Java|JavaProgramming) (has|have) methods");

i told this for example using string.but i'm unable to find how to declare like tht in that regular expression for pattern matching.so can any one suggest me how to sort out of this problem? in any other methds also possible to find out answer.kindly tell me experts.
Siva Vulchi
Ranch Hand

Joined: Nov 17, 2011
Posts: 30
Instead of

try like this

deepika deepi
Ranch Hand

Joined: Jan 23, 2012
Posts: 199
perfect.. superb i was struggling for this from yesterday. thanks a lot :-)))
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8008
    
  22

vijayalakshmi deepika wrote:i am having an doubt in pattern.compile while using that regular expression i have some doubt.

Well personally, I don't like the look of
"(Java|JavaProgramming)"
since I don't think it will do what you want (but I could be wrong). I think you be much better off with
"Java(Programming)?"
which is much more explicit.

It's also worth noting that "(...)" denotes a capturing group, which may or may not be what you want.
"(?:...)" denotes a non-capturing group, and it's what I usually use when I'm not sure (it's also likely to be slightly faster).

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
deepika deepi
Ranch Hand

Joined: Jan 23, 2012
Posts: 199
yes yar.i can use like that.Thanks
.but i'm using difffernet tags and different names also there.so for example purpose i used like that

i introduce an html tag in the string and i need to declare that string in my coding.so again the output window is emplty without any errors during run time.
i want to declare an string like this
String name = "<name>.*.</name>";

Pattern pattern = Pattern.compile("("+name+").*.(</name>).*.("+value+") methods");

instead of

Pattern pattern = Pattern.compile("(<name>).*.(</name>).*.("+value+") methods");

for the same string with slight variation

String text="<name>Java</name> has methods\n"+ "<name>JavaProgramming</name> have methods\n"+"<name>java</name> has methods";

and after getting the output like this
Words from the Text is:-
<name>Java</name> has methods
<name>JavaProgramming</name> have methods
<name>java</name> has methods
========================
Finded words from the Text is:-
<name>Java</name> has methods
<name>JavaProgramming</name> have methods
<name>java</name> has methods

can any one tell me what mistake i have some in the coding part


Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8008
    
  22

vijayalakshmi deepika wrote:can any one tell me what mistake i have some in the coding part

Yes, and I'm afraid it's using regexes at all.

They can do a lot of things, but they are not suited for parsing hierarchical tagged input, such as HTML/XML.

Luckily, there are a lot of parsers out there that are (just Google for SAX or DOM), although you may need to run the source through an XHTML converter first. I usually recommend JTidy, because that part is built-in, and it's based on HTMLTidy, which has been around since the KT extinction event - but there are many other alternatives out there in Internetland.

Winston
deepika deepi
Ranch Hand

Joined: Jan 23, 2012
Posts: 199
k experts.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: In java pattern matching how do declare string