942,793 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 1193
  • Java RSS
Feb 4th, 2010
0

Regex Help

Expand Post »
Hey guys, so i've posted here before about regex, and i was told that some of you may be able to check my regexes for errors after i have created them. I have one that looks like it should work to me, but it absolutely will not.

Java Syntax (Toggle Plain Text)
  1. public static String hitInput = "[CHAT WINDOW TEXT] [Sun Nov 29 11:34:13] Guardian of Water attacks Kyton's Rebuke [BH] : *hit* : (20 + 108 = 128 : Threat Roll: 3 + 108 = 111)";
  2. public static String hitRegex = "^.*\\]\\s+(.+)\\s+attacks\\s+(.+)\\s+:\\s\\*(?:[a-z][a-z]+)\\*\\s+:\\s.*\\+\\s(\\d+)\\s.+\\s+:\\s$";
  3.  
  4. public static Pattern hitPattern = Pattern.compile(hitRegex);
  5. public static Matcher hitMatcher = hitPattern.matcher(hitInput);
  6.  
  7. //the tester
  8.  
  9. public static void hitMatch() {
  10. boolean matchesHit = hitMatcher.matches();
  11. if (matchesHit == true) {
  12. String one = hitMatcher.group(1);
  13. String two = hitMatcher.group(2);
  14. String three = hitMatcher.group(3);
  15. String four = hitMatcher.group(4);
  16. System.out.println(one);
  17. }
  18. else System.out.println("DOESNT MATCH");
  19. }

anyone know why this doesnt match?

TJ
Similar Threads
Reputation Points: 10
Solved Threads: 3
Light Poster
tjsail is offline Offline
40 posts
since Nov 2009
Feb 4th, 2010
0
Re: Regex Help
Which terms are you trying to match here? Post a sample text along with the output you are expecting.

Also, the trick to creating complex regular expressions is to build the regular expression incrementally rather than writing it in a single go only to find it doesn't work as expected.
Last edited by ~s.o.s~; Feb 4th, 2010 at 1:33 pm.
Super Moderator
Featured Poster
Reputation Points: 3217
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,869 posts
since Jun 2006
Feb 4th, 2010
0
Re: Regex Help
the sample text is the variable hitInput. What I am looking to gather is The two names ("Guardian of Water" and "Kyton's Rebuke [BH]"), the *hit* (minus the *'s) and the first 108.

and when you say "build it incrementally" what do you mean?

TJ
Reputation Points: 10
Solved Threads: 3
Light Poster
tjsail is offline Offline
40 posts
since Nov 2009
Feb 5th, 2010
0
Re: Regex Help
Click to Expand / Collapse  Quote originally posted by tjsail ...
the sample text is the variable hitInput. What I am looking to gather is The two names ("Guardian of Water" and "Kyton's Rebuke [BH]"), the *hit* (minus the *'s) and the first 108.

and when you say "build it incrementally" what do you mean?

TJ

He means start small and work your way up.

junkstring
longerjunkstring
itsgettingreallycomplicated
substring
importantsubstring
reallyreallycomplicatedstringwithanimportantsubstring

Match the smaller pieces, then build upon it and put it together until you feel confident that you can match your longer pattern.
Last edited by apegram; Feb 5th, 2010 at 2:02 am.
Sponsor
Reputation Points: 318
Solved Threads: 135
LINQ!
apegram is offline Offline
550 posts
since Jan 2010
Feb 5th, 2010
0
Re: Regex Help
ah okay. i *think* i can do that for my lines... will give it a go and post back when i have tried.

in the mean time, has anyone been able to figure out why my original regex is not matching?

TJ
Reputation Points: 10
Solved Threads: 3
Light Poster
tjsail is offline Offline
40 posts
since Nov 2009
Feb 6th, 2010
0
Re: Regex Help
Never start off with your regular expression with greedy quantifiers unless you know what you are doing. The .* at the start of your expression gobbles up your entire line. Then it realizes that there are other patterns/characters to be matched i.e. the \\]\\s+(.+) and so the regex engine starts backtracking which is expensive.

> has anyone been able to figure out why my original regex is not
> matching?

Your regex is exhausted just before the "Threat" word i.e. your entire regex matches till the character 'T' of 'Threat'. Your use of $ at the end kills off the entire match and hence the engine doesn't report a match.

Also, your use of non-capturing parentheses (?:) confuses me; why use them? I've removed the non-capturing parentheses and added the .* at the end to get something like:
Java Syntax (Toggle Plain Text)
  1. public class ScrapRegexTests {
  2.  
  3. public static void main(final String[] args) {
  4. String hitInput = "[CHAT WINDOW TEXT] [Sun Nov 29 11:34:13] Guardian of Water attacks Kyton's Rebuke [BH] : *hit* : (20 + 108 = 128 : Threat Roll: 3 + 108 = 111)";
  5. String hitRegex = "^.*\\]\\s+(.+)\\s+attacks\\s+(.+)\\s+:\\s\\*([a-z][a-z]+)\\*\\s+:\\s.*\\+\\s(\\d+)\\s.+\\s+:\\s(.*)";
  6.  
  7. Pattern pat = Pattern.compile(hitRegex);
  8. Matcher matcher = pat.matcher(hitInput);
  9. if(matcher.matches()) {
  10. System.out.println("Regex matched : #" + matcher.group(1) + "#");
  11. System.out.println("Regex matched : #" + matcher.group(2) + "#");
  12. System.out.println("Regex matched : #" + matcher.group(3) + "#");
  13. System.out.println("Regex matched : #" + matcher.group(4) + "#");
  14. System.out.println("Unprocessed String : #" + matcher.group(5) + "#");
  15. } else {
  16. System.out.println("No match found!");
  17. }
  18. }
  19.  
  20. }
Quote originally posted by OUTPUT ...
Regex matched : #Guardian of Water#
Regex matched : #Kyton's Rebuke [BH]#
Regex matched : #hit#
Regex matched : #108#
Unprocessed String : #Threat Roll: 3 + 108 = 111)#
I'm sure you can carry on from here. I *think* there are better ways of writing the same thing but I'd defer my solution till you post your working solution.

HTH.
Last edited by ~s.o.s~; Feb 6th, 2010 at 5:18 am.
Super Moderator
Featured Poster
Reputation Points: 3217
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,869 posts
since Jun 2006
Feb 7th, 2010
0
Re: Regex Help
Once i get the output to look like this using a simple substring statement, would you recommend i test against Regex statements or test substrings against strings? i'm thinking regex, but i am looking for input.

Quote ...
[CHAT WINDOW TEXT] [Sun Nov 29 11:34:10]
Attack Of Opportunity : Soul Devourer attacks Kyton's Rebuke [BH] : *miss* : (13 + 113 = 126)
Reputation Points: 10
Solved Threads: 3
Light Poster
tjsail is offline Offline
40 posts
since Nov 2009
Feb 8th, 2010
0
Re: Regex Help
substring approach won't work given that the length of entire string along with the player names and the attack rating is not constant or known in advance. Go with the regex approach IMO.
Super Moderator
Featured Poster
Reputation Points: 3217
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,869 posts
since Jun 2006
Feb 8th, 2010
0
Re: Regex Help
Quote ...
[CHAT WINDOW TEXT] [Mon Feb 08 08:42:03] [Server] ===== Server 314 (1 player) =====
[60 (Sor29/RDD10/Pal1)] (TJ's Radiant Sorcerer)
[60 Sor29/RDD10/Pal1] TJ's Radiant Sorcerer
[60 Sor29/RDD10/Pal1] TJ's Radiant Sorcerer
[60 Sor29/RDD10/Pal1] TJ's Radiant Sorcerer
[60 Sor29/RDD10/Pal1] TJ's Radiant Sorcerer
[60 Sor29/RDD10/Pal1] TJ's Radiant Sorcerer
You are on server 314.
There are 35 current players on Higher Ground.
Given text as posted above, what is the easiest way to parse the pieces inside parens? I'm not sure how to code this in a regex, as the names will end up needing to be stored as variables, along with the classes. so i am not sure how i would go about calling the regex either.
Reputation Points: 10
Solved Threads: 3
Light Poster
tjsail is offline Offline
40 posts
since Nov 2009
Feb 8th, 2010
0
Re: Regex Help
> Given text as posted above, what is the easiest way to parse the pieces
> inside parens?


You can split the given string on spaces which would yield tokens like 60 , Sor29/RDD10/Pal1] , TJ's , Radiant Sorcerer . The way you would interpret these tokens depends on you.
Last edited by ~s.o.s~; Feb 8th, 2010 at 12:45 pm.
Super Moderator
Featured Poster
Reputation Points: 3217
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,869 posts
since Jun 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: interface
Next Thread in Java Forum Timeline: Add a Map ... <<Beginner Question>>





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC