Question:
pattern matching for java?
Doboy
2010-04-10 01:54:40 UTC
how do i compare a pattern of one string to another in java for example if i have the string
"ABBABCAC" it should make anything of that form such that all the A's are the same B's are the same and C's are the same are 1 character long each. so it should return true for something like
"KXXKXPKP"
Three answers:
ʃοχειλ
2010-04-10 04:52:27 UTC
There are two scenarios:



1) Analyzing the text ABBABCAC.

2) Matching a second text (KXXKXPKP) against the pattern extracted from (1).



The problem in (2) is easy if you know the pattern of (1) beforehand. But (1) cannot be done alone with reg exp itself. You could write a simple function to extract the regexp pattern of any similar sequence.



You could do (2) matching using back-reference in regexp; for your example (ABBABCAC), it would be (\w)(\w)\2\1\2(\w)\1\3 pattern. \n where n is 1, 2, 3, ... is the nth captured pattern or back-reference. You could analyze it as:



first there are two captured alphanumeric characters, then it would be second captured character (back-reference) followed by the first back-reference followed by the second back-reference; then there is another new captured character, and then it is another first back-reference and then the third back-reference. It works for any alphanumeric pattern like 4xx4xM4M. (It works with Visual C# .Net 2008 Regex class)
Dipen Thakkar
2010-04-10 09:28:38 UTC
Ya it is possible,

i can only give you the logic,

First take a char array of size 26 i.e.a char for each alphabet

initialize it with null or any other symbol..

then check ur 1st string(i.e in ur example ABBABCAC)

when first A is encountered store its corresponding value i.e K in array

when first B is encountered store its corresponding value i.e X in array

do d same till Z.

it will just need a for loop, starting from 65 to 97(Ascii value of A-Z)



then create a string with this corresponding array

i.e. check ascii value of 1 st char i.e 65 put array[0] for it..etc..

check whether this string is equal to entered string(KXXKXPKP)



if still problem exist plz contact me dipen.thakkar@ymail.com
deonejuan
2010-04-10 12:51:53 UTC
The only thing I can see that relates is you have

a String.length() ==8;

grouping of 4 matchers

and this is one case where Perl would amaze everyone. I don't see Java Pattern.matcher() able to.



about all you can do is look for 8-char words and

bit logic after that

which isn't too terribly hard if you have every coded a poker card game


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...