Results 1 to 13 of 13
 04022010, 01:54 AM #1Member
 Join Date
 Jan 2010
 Posts
 13
 Rep Power
 0
How to create regular expressions in Java
Hi,
i need to represent a String in the form of a regular expression in java. I need to somehow analyse the letters of the string and produce the expression representing that string.
For example if i have the string: abababab , i need to output (ab)*
another example if i have the string: aaaaab, i need to output (a)*b
Could anyone please advise.
Thanks
 04022010, 02:59 AM #2
You want to reduce the input String to an equivalent String that represents repeating characters or groups with special characters. That sounds *very* complicated.
The Java Tutorial. Read it.
 04022010, 07:04 AM #3
You'd have to write a parser, and a rather complicated one at that.
Why do you need this? There may be some easier way to achieve your goal.
db
 04022010, 07:59 AM #4
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,250
 Blog Entries
 7
 Rep Power
 24
You can't find such a regular expression uniquely; e.g. for the first example the regexes (abab)*, (abab)+ and abababab are also valid. I'd say that only the last regex is 'completely' valid because the other two can generate/accept other strings as well, but so does .* and is that considered a valid regex as well?
kind regards,
JosLast edited by JosAH; 04022010 at 08:46 AM.
 04022010, 09:27 AM #5Member
 Join Date
 Apr 2010
 Posts
 10
 Rep Power
 0
Hi, glad to c your question.
According to your description, I think it very hard to implement it. Like JosAH says, there is no unique expression with string "abababab", but dozens of.
 04022010, 01:25 PM #6Member
 Join Date
 Jan 2010
 Posts
 13
 Rep Power
 0
maybe i am approaching this incorrectly...
my goal is to convert a DFA to a regular expression, by traversing the transitions for every state..collecting the symbols along the way according to a specific algorithm, called Convert(G).
i have made my DFA in Java using Hashmaps, now all i need to do is somehow implement that Convert(G) algorithm on that DFA.
The goal of this algorithm is to end up with two states, and a regular expression representing the language the DFA accepts.Last edited by maz09; 04022010 at 07:27 PM.
 04022010, 01:48 PM #7
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,250
 Blog Entries
 7
 Rep Power
 24
Are you studying this lecture? The algorithm is in there.
kind regards,
Jos
 04022010, 02:00 PM #8Member
 Join Date
 Jan 2010
 Posts
 13
 Rep Power
 0
yes, thats the lecture, i need to implement this conversion in Java. my DFA is all set up, with transitions symbols as type String. However , this GNFA uses Regular expressions as transitions :S
 04022010, 03:08 PM #9
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,250
 Blog Entries
 7
 Rep Power
 24
 04022010, 04:30 PM #10Member
 Join Date
 Jan 2010
 Posts
 13
 Rep Power
 0
well i was just hoping for some guidance really, as my DFA at the moment has symbols on the transitions, i dont know how i would go about representing these symbols as regular expressions from a String ...
 04022010, 04:39 PM #11
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,250
 Blog Entries
 7
 Rep Power
 24
 04022010, 04:51 PM #12Member
 Join Date
 Jan 2010
 Posts
 13
 Rep Power
 0
thanks, i understand that, however, in terms of java... how would that combination take place?
If R1 were equal to ab.
and R2 were equal to ba for example, then i would just hardcode to remove these transitions and replace it by a string: "ab " + "" + " ba" . But i know thats wrong
I dont see how to put them in some sort of Set form so that the union, concatenation can be done to facilitate the [(R1)(R2)∗(R3)] U (R4) for instance.
 04022010, 05:13 PM #13
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,250
 Blog Entries
 7
 Rep Power
 24
There is nothing wrong about that transformation; you could add parentheses if you're not sure about the precedence of the operators, i.e. (ab)(ba) but all transformations can be done in String form. You want to end up with a final regular expression in String form don't you? Maybe because it's Friday but I don't see a problem with that approach.
kind regards,
Jos
Similar Threads

Using regular expressions for search
By carderne in forum New To JavaReplies: 9Last Post: 05242009, 04:58 AM 
Regular Expressions in java
By blue404 in forum Advanced JavaReplies: 2Last Post: 09262008, 03:43 AM 
Regular expressions from command prompt
By GenkiSudo in forum New To JavaReplies: 2Last Post: 09212008, 02:40 PM 
Using Quantifiers in regular expressions
By Java Tip in forum Java TipReplies: 0Last Post: 01102008, 11:43 AM 
Regular expressions quantifiers
By Java Tip in forum Java TipReplies: 0Last Post: 12252007, 12:18 PM
Bookmarks