• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

In java pattern matching how do declare string

 
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,

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());
}}
 
Marshal
Posts: 80281
432
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 22821
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
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
sorry experts,

am i not clear with my questions?
 
Rancher
Posts: 4804
7
Mac OS X VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not sure I understand your question.
If you mean what I think you as asking, how about....

 
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
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.
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Instead of

try like this

 
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
perfect.. superb i was struggling for this from yesterday. thanks a lot :-)))
 
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: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
 
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
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
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: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
Posts: 199
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
k experts.
 
If you are using a rototiller, you are doing it wrong. Even on this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic