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

Matching Positions in strings 1

Status
Not open for further replies.

fmuquartet

Programmer
Sep 21, 2006
60
US
Can someone help in understanding if it is possible to match a certain position in a string? Meaning, if there is a position in a string as shown below where you want to match what ever the value is for in that field.

#1.5#0017084C18E2009A00000005000073D700043CDE52C02A32#1192825536370#com.sap.portal.portal#sap.com/irj#com.sap.portal.portal#0175263#

In this case, I want to match on what ever appears where '0175263' is, since this value is dynamic and specific to the user logged into the system.
 
Seems that you have fields delimited by # symbols. The 'split' function is ideal for this:
Code:
my $string = '#1.5#0017084C18E2009A00000005000073D700043CDE52C02A32#1192825536370#com.sap.portal.portal#sap.com/irj#com.sap.portal.portal#0175263#';

my @pieces = split '#', $string;

print "$pieces[7]\n";
 
ishnid, thanks this certainly will and does work out, however I am need a regex that can do this if possible, do you know if this can be done via word-boundaries or somethings?
 
Why a regexp? Unless I've misunderstood your question, you need whatever text appears between the 7th and 8th '#' character, right? If so, 'split' is the ideal way of achieving that. If not, what criteria are you using to identify what you want to match?
 
Well, I am feeding data into an application that requires a definitive regex to match on this position, if I want to extract data in this field. I know it sounds weird, but that how the application works in order to report on this field.
 
If regex you must have:

Code:
[url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [blue]$string[/blue] = [red]"[/red][purple]#1.5#0017084C18E2009A00000005000073D700043CDE52C02A32#1192825536370#com.sap.portal.portal#sap.com/irj#com.sap.portal.portal#0175263#[/purple][red]"[/red][red];[/red]

[gray][i]# Diagraming Record[/i][/gray]
[gray][i]# 1 - #1.5[/i][/gray]
[gray][i]# 2 - #0017084C18E2009A00000005000073D700043CDE52C02A32[/i][/gray]
[gray][i]# 3 - #1192825536370[/i][/gray]
[gray][i]# 4 - #com.sap.portal.portal[/i][/gray]
[gray][i]# 5 - #sap.com/irj[/i][/gray]
[gray][i]# 6 - #com.sap.portal.portal[/i][/gray]
[gray][i]# 7 - #0175263#[/i][/gray]

[olive][b]if[/b][/olive] [red]([/red][blue]$string[/blue] =~ [red]m{[/red][purple]^(?:#[^#]*){6}#([^#]*)[/purple][red]}[/red][red])[/red] [red]{[/red]
	[url=http://perldoc.perl.org/functions/print.html][black][b]print[/b][/black][/url] [red]"[/red][purple]Found '[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]No 7th record[purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]
[red]}[/red]

- Miller
 
Assuming the field is always going to be numeric characters and is the last field you have, this should do it:
Code:
my ( $matched ) = $string =~ /(\d+)(?=#$)/;
 
ishnid,
Excellent, this field contains alpha-numeric characters as well, can we modify the regex for this?
 
change \d to \w

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
I do have a small problem that the regex is not picking up userids in for of frp100090 --is there something we can pass to it pick this up as well. I guess what I am asking if we can just say, matching on any alpha numeric character always in the 7th position of this string?
 
split still does exactly what you need, why not just split the string and run a regex on the atom you want

--Paul

Paul
------------------------------------
Spend an hour a week on CPAN, helps cure all known programming ailments ;-)
 
@Paul: He's not using Perl, just looking for regexp advice as the app he's using requires one.

@fmuquartet: did you make the change travs69 suggested?
 
Everyone:
I found that '#[^#]+#[^#]+#[^#]+#[^#]+#[^#]+#[^#]+#([^#]+)' worked perfect for matching any alpha-numeric character that appeared in the 7th position, thanks everyone for their immediate input on this subject.
 
MillerH's regexp from earlier does the same thing, but is shorter:
Code:
(?:#[^#]*){6}#([^#]*)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top