Solved

How to generate a string from a Regular Expression?

Posted on 2009-06-28
12
820 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 84

Expert Comment

by:ozo
Comment Utility
what kind of correspondence are you talking about?
0
 
LVL 39

Assisted Solution

by:abel
abel earned 20 total points
Comment Utility
> 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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

Expert Comment

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

Expert Comment

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

Assisted Solution

by:ozo
ozo earned 20 total points
Comment Utility
0
 
LVL 39

Expert Comment

by:abel
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Periodically we have to update or add SSL certificates for customers. Depending upon your hosting plan you may be responsible for the installation and/or key generation. In the wake of Heartbleed many sites were forced to re-key. We will concen…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

728 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now