Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Tough regular expression question

Status
Not open for further replies.

dnmilne

Programmer
Jan 17, 2008
1
NZ
Hi there,

I've been struggling with this seemingly simple regular expression problem. I'm hoping someone can show me how dumb I've been:

take the string {{aaa{{bbb}}ccc}}

What I need is a regex that would match the {{bbb}} part, but not the whole thing. Basically, anything in double brackets as long as it doesnt have nested stuff in double brackets within it. The tricky part is that its allowed to contain single brackets.

Some examples:
{{aa}} - want to match "aaa"
{{aaa{{bbb}}}} - want to match "bbb" (not "aaa{{bbb"!!!)
{{aaa{{bbb}}ccc}} - still want to match "bbb"
{{aaa{{b{bb}}ccc}}- want to match "b{bb"

Any suggestions?
 
Hi,

Try

Code:
$str =~ /^\{\{.+\{{2}(.+)?\}(2).+$/ ;
print $1 ;

ps: Not Tested.

--------------------------------------------------------------------------
I never set a goal because u never know whats going to happen tommorow.
 
This one
Code:
regexp {{{(\w*?{?\w*?}?\w*?)}}} $string -> result
will match as you require with 0 or 1 (not more) single open braces and subsequent 0 or 1 single closed braces embedded within the innermost sequence of word chars contained within double open and closed braces.
Your definition and examples were not sufficiently well defined to insure that this is exactly what you were looking for, but it works for the examples provided.

Franco
: Online tools for structural design
: Magnetic brakes for fun rides
: Air bearing pads
 
Check out the following:

Code:
[url=http://perldoc.perl.org/functions/use.html][black][b]use[/b][/black][/url] [green]strict[/green][red];[/red]

[olive][b]while[/b][/olive] [red]([/red]<DATA>[red])[/red] [red]{[/red]
	[url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [blue]$str[/blue] = [blue]$_[/blue][red];[/red]
	
	[url=http://perldoc.perl.org/functions/print.html][black][b]print[/b][/black][/url] [blue]$str[/blue][red];[/red]
	
	[olive][b]if[/b][/olive] [red]([/red][blue]$str[/blue] =~ [red]m/[/red][purple][purple][b]\{[/b][/purple][purple][b]\{[/b][/purple]([/purple]
[purple]		(?:[/purple]
[purple]			[^{}]+[/purple]
[purple]		|[/purple]
[purple]			[purple][b]\{[/b][/purple](?![purple][b]\{[/b][/purple])[/purple]
[purple]		|[/purple]
[purple]			[purple][b]\}[/b][/purple](?![purple][b]\}[/b][/purple])[/purple]
[purple]		)*[/purple]
[purple]		)[purple][b]\}[/b][/purple][purple][b]\}[/b][/purple][/purple]
[purple]	[/purple][red]/[/red][red]sx[/red][red])[/red] [red]{[/red]
		[black][b]print[/b][/black] [red]"[/red][purple][purple][b]\t[/b][/purple]Matches '[blue]$1[/blue]'[purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]
	[red]}[/red] [olive][b]else[/b][/olive] [red]{[/red]
		[black][b]print[/b][/black] [red]"[/red][purple][purple][b]\t[/b][/purple]Doesn't Match[purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]
	[red]}[/red]
[red]}[/red]

[teal]__DATA__[/teal]
[teal]{{aa}}            - want to match "aaa"[/teal]
[teal]{{aaa{{bbb}}}}    - want to match "bbb" (not "aaa{{bbb"!!!)[/teal]
[teal]{{aaa{{bbb}}ccc}} - still want to match "bbb"[/teal]
[teal]{{aaa{{b{bb}}ccc}}- want to match "b{bb" [/teal]
[teal]{{aaa{{b{b}b}}ccc}}- want to match "b{bb" [/teal]
[tt]------------------------------------------------------------
Pragmas (perl 5.10.0) used :
[ul]
[li]strict - Perl pragma to restrict unsafe constructs[/li]
[/ul]
[/tt]

- Miller
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top