There is a string variable containing number data with dots , say $x = "OP/1.1.2/DIR"; . The position of the number data may change at any circumstance by user desire by modifying it inside the application , and the slash bar may be changed by any other character ; but the dotted number data is mandatory. So how to extract the dotted number data , here 1.1.2, from the string ?
There is a string variable containing number data with dots , say $x =
Share
Use a regular expression:
Breakdown:
\d+look for one or more digits\.a literal decimal.character\d+followed by one or more digits again(...)*this means match 0 or more occurrences of this pattern(?:...)this tells the engine not to create a backreference for this group (basically, we don’t use the reference, so it’s pointless to have one)You haven’t given much information about the data, so I’ve made the following assumptions:
If any of these assumptions are incorrect, you’ll have to modify the regular expression.
Example usage: