Pattern Matching

Pattern Matching...


This is a very useful and powerful tool to have in your arsnal. It is power packed. I use this all the time to chop up strings. In essence it is substring on STEROIDS. I personally think this is proof Brian Fox is a genius.

There are two symbols that you need to remember to use this little trick, the Pound Sign (#) and the Percent Sign (%)

The pound sign (#) matches stuff in the front of the string OR as I like to put it knocks stuff off the front of the string. A good way to remember this is... where does a pound sign go... it's in the front of the string. So the stuff you are going to keep is in the back of the string. More importantly the stuff you are going to knock of is in the front of the string... remember that... it's important. With the pound sign, the stuff you are going to trash is in the front of the string.

The percent sign (%) matches stuff in the back of the string. Or as I like to say... it knocks stuff off the back of the string. Again you can remember this because the percent sign always goes in the back. Therefore, the stuff you are going to keep is in the front of the string. Again and more importantly the stuff you are going to knock off is in the back of the string... that's important too. The stuff you are going to trash is in the back of the string.

Now the format for these two jewels... it starts, like all variable references, with the old dollar sign - or as I like to call it - the String sign. Next is the opening curly brace. All the work is done inside curly braces. Next is the variable itself. So far the construct looks like this:

$ ${myVar...}

There are two parts after the variable and this depends on wheather you are pulling from the front of the string or the back. So lets draw a good old example. We have a string of I like cake. We want to extract the cake because that's what we all care about anyway. So we are going to use the pound sign because it will trash the stuff in the front of the string. The stuff we don't care about.

$ myVar="I like cake."

One lump or two... how many symbols you use determines how much you keep or knock off. One pound sign matches the minimum then gives up while two matches as much as it can. So we want to take the cake... out of the string. The best place to split is is on the space. Since there are two spaces in our way we want to match the most. In regex they call this being greedy. So we need to use two pound signs so we can match the most in the front of the string (knock it off) to get to our cake.

$ ${myVar##...

Now we need to get the pattern right with some globbing. What we want to match on is the space. So that needs to go in our construct. The second thing we need is to tell it where to match this space at. That's the globbing part... i.e. the astrisk (*). This is the important part to remember as stated earlier. We are knocking stuff off the front of the string with the pound signs. That means our pattern is going to address what we are trashing. So the astrisk goes first, then the space, because that's where the matching stuff is. Remember, we are trashing the stuff in the front of the string so the astrisk goes in the front. That's why it's important... now this is our construct.

$ ${myVar##* }

And this is that construct in action...

$ myVar="I like cake"
$ echo ${myVar##* }
$ cake

Now you know the rest of the story.

CREATED 2017-04-18 15:32:24.0

00-29-99

UPDATED 2020-03-31 10:18:38.0

More Examples


  • ${var#pattern} - Match the shortest part of the front
    • Match the shortest part of the pattern in the front of the string.
  • ${var##pattern} - Match the longest part of the front
    • Match as much of the pattern against the longest part of the front of the string.
  • ${var%pattern} - shortest match in the back.
    • Match the pattern against the shortest part of the back half of the string then return the rest.
    • greeting="Hello my name is Kenneth." echo "${greeting%neth.}ny."
      Hello my name is Kenny.

      Suppose we had a string that is a formal greeting as in:

      Could be made into an informal greeting with ${greeting%neth.}. This would chop the neth. off the end of the string and return the rest. Adding an ny to the end of it would make an informal greeting.

  • ${var%%pattern} - longest match in the back.
    • Match the pattern against the longest part of the back half of the string and return the rest.
    • echo ${greeting%%K*.}Bob.
      Hello my name is Bob.

      This one is good for Regex patterns. Suppose we wanted to change the name completely. This can be accomplished with: ${greeting%%K*.} which would return "Hello my name is ". Then add another name... say Bob.

CREATED 2020-03-31 09:48:09.0

010-00-00-CB

UPDATED 2020-03-31 10:18:47.0

Knowledge

L
I
N
K
S

DBID: db.wam

Page Server: Ithica

©2012 Leistware Data Systems

      Hello anonymous