Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

In java pattern matching how do declare string

 
deepika deepi
Ranch Hand
Posts: 199
  • Mark post as helpful
  • send pies
  • 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());
}}
 
Campbell Ritchie
Sheriff
Posts: 48363
56
  • Mark post as helpful
  • send pies
  • 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.
 
Rob Spoor
Sheriff
Pie
Posts: 20492
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
sorry experts,

am i not clear with my questions?
 
Pat Farrell
Rancher
Posts: 4678
7
Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
Siva Vulchi
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Instead of

try like this

 
deepika deepi
Ranch Hand
Posts: 199
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
perfect.. superb i was struggling for this from yesterday. thanks a lot :-)))
 
Winston Gutkowski
Bartender
Pie
Posts: 10084
55
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • 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
  • 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
Pie
Posts: 10084
55
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
k experts.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic