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

perl index function 2

Status
Not open for further replies.

GrahamBright

Programmer
Oct 31, 2003
65
AT
Hi,

I need to build up a regular expression for the perl index fuction. Basically I am trying to search for a substring with variable spaces which is then followed by alpanumeric characters:

Example:

1234567890 abcdefg

Anyone know how I can include a regular expression to

if($pos2=index($myline," ",$pos1))

Thanks
Graham.
 
The following regexp will match alphanumerics followed by spaces followed by alphanumerics:
Code:
/\w+\s+\w+/

I'm not sure what you're trying to do with the index function though. If you're trying to find out the position of where the match occurred in the string, have a look for @LAST_MATCH_START in perlvar. That'll give it to you.
 
Take advantage of the global match variable in order to determine the last position in a regex match. However, I suspect that you could solve whatever you're trying to do solely with a regex.

Code:
[url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [blue]$str[/blue] = [red]'[/red][purple]1234567890        abcdefg      foo  bar[/purple][red]'[/red][red];[/red]

[olive][b]if[/b][/olive] [red]([/red][blue]$str[/blue] =~ [red]m/[/red][purple]([purple][b]\s[/b][/purple]+[purple][b]\w[/b][/purple]+)[/purple][red]/[/red][red]g[/red][red])[/red] [red]{[/red]
	[url=http://perldoc.perl.org/functions/print.html][black][b]print[/b][/black][/url] [red]"[/red][purple]Match '[blue]$1[/blue]' ended at position [/purple][red]"[/red] . [url=http://perldoc.perl.org/functions/pos.html][black][b]pos[/b][/black][/url][red]([/red][blue]$str[/blue][red])[/red] .  [red]"[/red][purple][purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]
[red]}[/red]

- Miller
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top