Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to generate a string from a Regular Expression?

Posted on 2009-06-28
12
Medium Priority
?
834 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 80 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
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 
LVL 85

Expert Comment

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

Assisted Solution

by:abel
abel earned 80 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 85

Assisted Solution

by:ozo
ozo earned 80 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction This article explores the design of a cache system that can improve the performance of a web site or web application.  The assumption is that the web site has many more “read” operations than “write” operations (this is commonly the ca…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
Suggested Courses

963 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