Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion Groups
Mathematics
General TopicsResearchOperations ResearchStatisticsMathematical LogicNumerical AnalysisUndergraduate MathAlgebra HelpRecreational Math
Math Software
MapleMathematicaMATLABScilabSASSPSS

Re: inverse regexp



Tip: Looking for answers? Try searching our database.



You are accessing this site in a read-only mode. For full access to all member benefits, including message posting, please login or register. Registration is completely free, simple, and takes only a few seconds.

Login | Free MathKB.com registration | Whole discussion thread

The message you are replying to and its parents are listed in the reverse order with the most recent posts first. This might not be the whole discussion thread. To read all the messages in this thread please click here.

Re: inverse regexp

Jason Breslau26 Jul 2008 17:56
"Bruno Luong" <b.luong@fogale.fr.findthecountry> wrote in
message <g6eq9v$cii$1@fred.mathworks.com>...

> Impressive! This kind of code reminds me now why I never get
> around to understand fully regexp syntax.
>
> Bruno

I'm sorry if I turned you off of it.  Regular Expressions
are really fun, and can be useful, too, if used properly.

The recursive example I used was modified from an example in
Jeffrey Friedl's "Mastering Regular Expressions", which is a
fantastic book on regular expressions.  I couldn't recommend
it more: http://regex.info/

-=>J

Bruno Luong26 Jul 2008 09:21
Jason Breslau <tendiamonds@mathworks.com> wrote in message

>  >> str = 'Johan and Mary';
>  >> rex = '(?<husband>[a-z]([aeiou](?=[a-z])[^aeiou])*) and (?<wife>\S+)';
>  >> s = regexpi(str,rex,'names');
>  >> levelN = '\(([^()]|(??@levelN))*\)';
>  >> regexprep(rex, '\(\?<(\w*)>([^()]|(??@levelN))*\)', '${s.($1)}')

Impressive! This kind of code reminds me now why I never get
around to understand fully regexp syntax.

Bruno

Jason Breslau26 Jul 2008 02:49
Hi Jean-Yves,

Interesting problem.

To solve the simple case, in which there are only named tokens and
literal strings, you can use regexprep to reconstruct the match, but as
Ashish pointed out, you will need 'split' to recreate the portions of
the initial string that are not matched.

To make things simpler, you can restrict your pattern to named tokens
that do not contain nested parentheses:

>> str = 'Johan and Mary';
>> rex = '(?<husband>\S+) and (?<wife>\S+)';
>> s = regexp(str,rex,'names');
>> regexprep(rex, '\(\?<(\w*)>.*?\)', '${s.($1)}')

ans =

Johan and Mary

See how this fails with a pattern using nested parentheses:

>> str = 'Johan and Mary';
>> rex = '(?<husband>[a-z]([aeiou](?=[a-z])[^aeiou])*) and (?<wife>\S+)';
>> s = regexpi(str,rex,'names')

s =

    husband: 'Johan'
       wife: 'Mary'

>> regexprep(rex, '\(\?<(\w*)>.*?\)', '${s.($1)}')

ans =

Johan[^aeiou])*) and Mary

To handle nested parentheses, you need to use a recursive pattern:

>> str = 'Johan and Mary';
>> rex = '(?<husband>[a-z]([aeiou](?=[a-z])[^aeiou])*) and (?<wife>\S+)';
>> s = regexpi(str,rex,'names');
>> levelN = '\(([^()]|(??@levelN))*\)';
>> regexprep(rex, '\(\?<(\w*)>([^()]|(??@levelN))*\)', '${s.($1)}')

ans =

Johan and Mary

-=>J

Jean-Yves Tinevez25 Jul 2008 17:46
I was wondering if anyone wrote or met a function that was
inverting regexp.

For instance, let's suppose I want to extract tokens out a a
string. I can do that with regexp:

>> str = 'Johan and Mary';
>> rex = '(?<husband>\S+) and (?<wife>\S+)';
>> s = regexp(str,rex,'names')

s =

   husband: 'Johan'
      wife: 'Mary'

Now I would like to do the inverse transformation, and have
a function that would do this:

n = iregexp(rex,s)

and would output

n = 'Johan and Mary'

Has anyone ever met something like this? Or would have an
idea on how to do it?
jy

Quick links:

 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage




©2010 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.