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!

Regex help 2

Status
Not open for further replies.

lerdalt

MIS
Oct 30, 2001
1,120
US
ok..got something that is causing me some grief in understanding. Translation rule for CVoice. What the book is saying is this:

rule 1 /\(9\)\([^01].*\)/ /\11408\2/

input of 95550134
output of 914085550134

What has me stumped is how ([^01].*\) matches the 5550134. Hopefully someone can explain it better to me.
 
Got the answer:
The first thing we do is break the rule down into two portions, the matched string and the replacement string. We, identify those two components by finding the two sets of forward slashes. Here's what we have:



Matched String: /\(9\)\([^01].*\)/

Replacement String: /\11408\2/



Parentheses create "sets" in the matched string, which can then be referenced in the replacement string. Also, a backslash is given before a parenthesis to negate the parenthesis being treated as a special character. So, here are our two sets:



Set 1: 9

Set 2: [^01].*



Set 1 is simply the digit 9.

Set 2 begins with a digit other than 0 or 1. That's what [^01] means. The "^" symbol in brackets means the digit being matched cannot be one of those digits. The North American Number Plan says that the first digit of an area code or an office code cannot begin with a 0 or a 1 (thus the [^01]). The period character matches any digit, and the asterisk character means match the previous expression (which was a period) zero or more times. So, [^01].* can match a digit string, not beginning with a 0 or a 1, followed by one or more other digits.



This matched string would therefore match 95550134, since the string begins with a 9; the next digit is not a 0 or a 1; and those two digits are followed by one or more digits (that is, 550134).



Now, for the replacement string, the \1 refers to Set 1 (which was a 9 in this example). So, the replacement string begins with a 9.

The replacement string then contains the digits 1408.

Finally, the replacement string refers to Set 2 with the \2 (which was 5550134 in our example).



When we put all these components together, we get a replacement string of 914085550134. So, basically, what this did was insert a 1 and an area code after the access code of 9 and before the 7-digit dial string of 5550134.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top