• Post Reply Bookmark Topic Watch Topic
  • New Topic

Problem in regex  RSS feed

 
Greenhorn
Posts: 24
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Above code generates output according to previous pattern("\\d") why so?
 
Ranch Hand
Posts: 491
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Run this code and tell me what you observe.
 
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are making two assumptions that are on very shaky ground:

1) that a Matcher is any way dependent on the Pattern it was derived from after the Matcher has been created

2) that assigning a new Pattern to the "p" reference would somehow influence matchers derived from another Pattern object that was previously assigned to p

While #1 might or might not be correct, that really makes no difference, because #2 is clearly false. How would the Matcher even know that a new Pattern has been assigned to p? The Matcher only depends on the object that was assigned to p, not on p itself.

Also note that you're confusing yourself by writing line 9 as you did. It should properly read "p=Pattern.compile(...)" - which makes it clear that its result can't possibly influence the previous Pattern assigned to p.
 
Greenhorn
Posts: 5
Eclipse IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Part of the problem here also may be that in Java, when you reset p to be a newly compiled object, it's reference changed, and the matcher is still pointing to the original p. In Java everything is passed by value, so the Pattern that the Matcher uses does not change when you change the value of the reference-type Pattern p. Essentially, what you are trying to do will not work, ever, and it's not because Java is broken, rather it's because your code is not the Java-way of doing things. You really need to create two Patterns and two Matchers. But you might step back even further and ask yourself, what are you really trying to do, and why? You may find that what you are trying to do can be done in a better way.
 
Nathan Lane
Greenhorn
Posts: 5
Eclipse IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I thought that a code example might illustrate my point:


If you run this example, you get the following output:


Why, you may ask? Because the reference to Name myName is not retained inside of the NamePrinter instance, only the original value is retained.
 
MrKamal Joshi
Greenhorn
Posts: 24
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks all.
 
H Paul
Ranch Hand
Posts: 491
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If this help.

This is what some one once explained to me by analogy at a high view:

Pattern.compile (regex) is a kind of SQL Statement (or precompiled stored procedure strored on DB)
Matcher is a kind of ResultSet or RecordSet

So until you execute the command - RegEx or SQL - you will not get the (new) result.

For database, result is rs = statement.execute
For regex, result is matcher = p.matcher

Like-wise for XPath

XPathExpression xPathExpression = xPath.compile("xpathex");
result = xPathExpression.evaluate(...);
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!