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 Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

substitute white space 1

Status
Not open for further replies.

Tdlearner

Technical User
Jun 17, 2004
13
US
Hi,

The following regex removes white space from head and tail of the line.

perl -pe 's/^\s+|\s+$//g'

How come it removes both ? It should just remove front because of presence of or (|).

Looking it in boolean context, remove space from front or remove space from end.
The first substitution evaluates to be true then how come it evaluates the second substitution after "|" ?

Does presence of global substitution forces it ?

thanks,
 
No forcing going on. global just encourages it to try the regex multiple times.

Because of the or and global, it's equivalent to:

Code:
perl -pe 's/^\s+//g; s/\s+$//g;'

Which logically, is equivalent to:

Code:
perl -pe 's/^\s+//; s/\s+$//;'

- Miller
 
Thanks Miller,

But in a way presence of g (global) is making or behave like and right ?

Without g it would have simply remove space at beginning only not at the end .

Pl correct if I inferred it wrong.
 
The presense of g (global), allows the substitution to happen more than once. Say you have the following code:

Code:
[url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [blue]@strings[/blue] = [red]([/red]
	[red]"[/red][purple]No Spacing[/purple][red]"[/red],
	[red]"[/red][purple]   Pre spacing, but no post[/purple][red]"[/red],
	[red]"[/red][purple]No pre spacing, but yes post   [/purple][red]"[/red],
	[red]"[/red][purple]   Pre spacing and post spacing   [/purple][red]"[/red],
[red])[/red][red];[/red]

[olive][b]foreach[/b][/olive] [black][b]my[/b][/black] [blue]$string[/blue] [red]([/red][blue]@strings[/blue][red])[/red] [red]{[/red]
	[url=http://perldoc.perl.org/functions/print.html][black][b]print[/b][/black][/url] [red]"[/red][purple]old string = '[blue]$string[/blue]'[purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]

	[olive][b]if[/b][/olive] [red]([/red][blue]$string[/blue] =~ [red]s/[/red][purple]^[purple][b]\s[/b][/purple]+|[purple][b]\s[/b][/purple]+$[/purple][red]/[/red][purple][/purple][red]/[/red][red])[/red] [red]{[/red]
		[black][b]print[/b][/black] [red]"[/red][purple]   new string is = '[blue]$string[/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]   no change[purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]
	[red]}[/red]

	[black][b]print[/b][/black] [red]"[/red][purple][purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]
[red]}[/red]

What would the output be? What surrounding spacing would be left?

The answer is the 4th string would have post spacing still, because the pre spacing shortcutting it and there was no global matching. However, with global matching, once the pre spacing is removed, then the regex marches on down the string and is free to match the ending spacing the second time through.

Please note that this is a special case regular expression. It is not very common to have an or expression in a regex where the different sections do not share anything. It's much more common to see an or expression inside a non-capturing group (?: ). However, this is simply the accepted way of stripping pre and post spacing quickly, although I tend to use a function.

- Miller
 
Thanks a lot Miller for such a nice explanation.

You have a deep understanding of PERL and more importantly,
thanks for sharing. Thats really nice of you.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top