Consider the following strings:
1) Scheme ID: abc-456-hu5t10 (High priority) *****
2) Scheme ID: frt-78f-hj542w (Balanced)
3) Scheme ID: 23f-f974-nm54w (super formula run) *****
and so on in the above format - the parts in bold are changes across the strings.
==> Imagine I've many strings of format Shown above. I want to pick 3 substrings (As shown in BOLD below) from the each of the above strings.
- 1st substring containing the alphanumeric value (in eg above it's "abc-456-hu5t10")
- 2nd substring containing the word (in eg above it's "High priority")
- 3rd substring containing * (
IF
* is present at the end of the stringELSE
leave it )
How do I pick these 3 substrings from each string shown above? I know it can be done using regular expressions in Perl... Can you help with this?
You could use a regular expression such as the following:
So for example:
prints
You could do something like this:
The key thing is the Regular expression:
Which breaks up as follows.
The fixed String "Scheme ID: ":
Followed by one or more of the characters a-z, 0-9 or -. We use the brackets to capture it as $1:
Followed by one or more whitespace characters:
Followed by an opening bracket (which we escape) followed by any number of characters which aren't a close bracket, and then a closing bracket (escaped). We use unescaped brackets to capture the words as $2:
Followed by some spaces any maybe a *, captured as $3:
String 1:
String 2:
String 3:
Well, a one liner here:
Expanded to a simple script to explain things a bit better:
Though if it were anything other than formatting, I would implement a main loop to handle files and flesh out the body of the script rather than rely ing on the commandline switches for the looping.
Long time no Perl