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

regexp 1

Status
Not open for further replies.

reggler

Programmer
Nov 13, 2008
63
CA
Hi there,

I've been playing around with regular expression but seem not to be able to figure out how it works... :(
I have a string like:
"OrderCode=RS910-HI-N-S1-TX01-TX" and i need to extract whatever is betweenm
the '=' and the first '-' so in this case it would be 'RS910'
How would i do this correctly?

Thank you!
Ron
 
Hi reggler,

I showed you once, how to extract the number of iterations in this thread and you forgot to give me a star :-(

For your above problem it's similar. It could be done with this code
Code:
[COLOR=#804040][b]set[/b][/color] mystring [COLOR=#ff00ff]"OrderCode=RS910-HI-N-S1-TX01-TX"[/color]
[COLOR=#804040][b]set[/b][/color] result [[COLOR=#804040][b]regexp[/b][/color] {OrderCode\s*=\s*(.+)-\S+-\S+-\S+-\S+-\S+} [COLOR=#008080]$mystring[/color] match first_part_of_code]

[COLOR=#804040][b]puts[/b][/color] [COLOR=#ff00ff]"Matching Result=$result"[/color]
[COLOR=#804040][b]puts[/b][/color] [COLOR=#ff00ff]"This was matched: '$match'"[/color]
[COLOR=#804040][b]puts[/b][/color] [COLOR=#ff00ff]"First Part Of OrderCode (extracted): '$first_part_of_code'"[/color]

Output
Code:
Matching Result=1
This was matched: 'OrderCode=RS910-HI-N-S1-TX01-TX'
First Part Of OrderCode (extracted): 'RS910'

You get the result in $first_part_of_code
 
Or use better this regex
Code:
[COLOR=#804040][b]set[/b][/color] result [[COLOR=#804040][b]regexp[/b][/color] {OrderCode\s*=\s*(\S+)-\S+-\S+-\S+-\S+-\S+} [COLOR=#008080]$mystring[/color] match first_part_of_code]
The difference is that:
\.+ means any character once or more and
\S+ means any not space character once or more
 
Hi mikrom,

Yes right, sorry I didn't remember this :$
Thanks very much for your help tho!
Uhm but anyways, with your code I got
"can't read "match": no such variable"
I don't know why this appears, my code looks like this:
Code:
        set result [regexp {OrderCode\s*=\s*(.+)-\S+-\S+-\S+-\S+-\S+} finaltest::product(orderCode) match first_part_of_code]
        $GUI::twidget insert end "Matching Result=$result" error
		$GUI::twidget insert end "This was matched: '$match'" error
		$GUI::twidget insert end "First Part Of OrderCode (extracted): '$first_part_of_code'" error
May you have any suggestionas for this problem? I promise I'll award you with a star ;)

Ron
Hours of planing can save weeks of coding
 
What is finaltest::product(orderCode). I sa function that returns the OrderCode?
You need to pass variable value with dollar $

If you try in my example instead of
Code:
[COLOR=#804040][b]set[/b][/color] result [[COLOR=#804040][b]regexp[/b][/color] {OrderCode\s*=\s*(\S+)-\S+-\S+-\S+-\S+-\S+} [COLOR=#008080]$mystring[/color] match first_part_of_code]
this without $
Code:
[COLOR=#804040][b]set[/b][/color] result [[COLOR=#804040][b]regexp[/b][/color] {OrderCode\s*=\s*(\S+)-\S+-\S+-\S+-\S+-\S+} [COLOR=#008080]mystring[/color] match first_part_of_code]

you get your error
Code:
Matching Result=0
can't read "match": no such variable
    while executing
"puts "This was matched: '$match'""
    (file "regexp2.tcl" line 5)

And don't forget the STAR !
:)
 
there you go :) thanks

Ron
Hours of planing can save weeks of coding
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top