Solved

How to generate a string from a Regular Expression?

Posted on 2009-06-28
12
823 Views
Last Modified: 2012-05-07
I have a regular expression using which i need to generate its corresponding string token..
0
Comment
Question by:unicorn_rm
  • 5
  • 2
  • 2
  • +1
12 Comments
 
LVL 6

Expert Comment

by:Ravi Kalla
ID: 24734483
A regular expression can generate infinate strings. So, a single string token can't be generated to it. Do you want to find the strings that satisfies the Regular Expression from a list of strings?
Mention you requirement in detail.
 
0
 
LVL 39

Expert Comment

by:abel
ID: 24744034
Perhaps you mean to show the string representation of a regular expression in memory? The following code snippet shows how to print the internal regular expression as it was passed into the regex  constructor. It prints the following:

[hH]ello\s+[wW]orld$

// output the internal regular expression
Regex re = new Regex(@"[hH]ello\s+[wW]orld$");
Debug.WriteLine(re.ToString());

Open in new window

0
 

Accepted Solution

by:
tazmaster earned 20 total points
ID: 24887998
Primarily regular expressions are used on Strings to extract bits of it that match the regular expression that you've specified. if you're interested in identifying all of the various Strings that can match a regular expression rather than identifying them in an already existing one then it can be tricky, if you have a well defined expression then you may be able to limit the output, otherwise you might as well use the brute force method and create all of the conceivable combinations of characters and apply the regular expression to them to see if it matches or not.
0
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 
LVL 84

Expert Comment

by:ozo
ID: 24888010
what kind of correspondence are you talking about?
0
 
LVL 39

Assisted Solution

by:abel
abel earned 20 total points
ID: 24895361
> create all of the conceivable combinations of characters and apply the > regular expression to them to see if it matches or not.
that would be funny, say we do not consider unicode, but only us-ascii, which has 128 codepoints (127, plus codepoint 0), and we try the following regex: ".*a.*" (yes, I know it can be written as "a" too, I just want to make a point with the .*) . Then we apply all possible combinations.... let's see (each X is any character, a is a):

a = 1
aX = 128 + 1 = 129
Xa = 128 + 129 = 257
aXX = 128 * 128 + 257 = 16641
XaX = 128 * 128 + 16641 = 33025
XXa = ....
 aXXX
XaXX
XXaX
XXXa = 8438017

I know, not that much, but add on more character and an average computer will not have enough memory: 1,350,615,297. yet another character and your disk cannot copy with it anymore... Not really a tempting idea to use brute force, I think..., but interesting nonetheless.

As ozo pointed out in the first answer, there are often an infinite amount of strings that can match a certain expression (the above is one such). Which one would you return?

-- Abel --


0
 

Expert Comment

by:tazmaster
ID: 24900408
i wasn't making a serious suggestion... i was attempting to demonstrate the futility of attempting it.
0
 
LVL 39

Expert Comment

by:abel
ID: 24902575
;-)
then I think we made the right point together :D
0
 
LVL 84

Assisted Solution

by:ozo
ozo earned 20 total points
ID: 24902652
0
 
LVL 39

Expert Comment

by:abel
ID: 24902771
Nice one, I particularly like the Explain module (I don't really see the point in Genex... but hey, if that's the request ;-)

I think these answers are very valuable and all that for 50p ;-) ....but the q. has been abandoned since the start...
0
 
LVL 39

Expert Comment

by:abel
ID: 25284251
This question received ample coverage, multiple experts come to the same conclusion: "cannot be done" unless you have infinite time/space.

http:#24744034 (abel) shows how to tell the string repres. of a regex
http:#24887998 (tazmaster) explains the futility of getting matching strings
http:#24895361 (abel) elaborates on that
http:#24902652 (ozo) giving another angle with Explain and GenEx

Suggestion: equal split between http:#24887998 (tazmaster), http:#24895361 (abel) and http:#24902652 (ozo).
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The greatest common divisor (gcd) of two positive integers is their largest common divisor. Let's consider two numbers 12 and 20. The divisors of 12 are 1, 2, 3, 4, 6, 12 The divisors of 20 are 1, 2, 4, 5, 10 20 The highest number among the c…
Meet the world's only “Transparent Cloud™” from Superb Internet Corporation. Now, you can experience firsthand a cloud platform that consistently outperforms Amazon Web Services (AWS), IBM’s Softlayer, and Microsoft’s Azure when it comes to CPU and …
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question