Sometimes you will find it useful to take strings apart or convert them into numbers, and these parsing functions can help you accomplish these tasks. String Subset accesses a particular section of a string. It returns the substring beginning at offset and containing length number of characters. Remember, the first character's offset is zero (see Figure 9.18). Figure 9.19 shows an example of how String Subset can be used to return a subset of an input string. Figure 9.18. String Subset
Figure 9.19. String Subset used to return a subset of an input string
Scan From String, the "opposite" of Format Into String, converts a string containing valid numeric characters (0 to 9, +, -, e, E, and period) to numeric data (see Figure 9.20). This function starts scanning the input string at initial search location and converts the data according to the specifications in format string (to learn more about the specifications, see the LabVIEW manuals or "String Function Overview" in the Online Reference). Scan From String can be resized to convert multiple values simultaneously. Figure 9.20. Scan From String
a, b, c, d, e, f, A, B, C, D, E, and F are valid characters if a hex format is specified, and comma may be valid if it is the localized decimal point. In this example, Scan From String converts the string "VOLTS DC + 1.28E + 2" to the number 128.00 (see Figure 9.21). It starts scanning at the eighth character of the string (which is the + in this caseremember that the first character offset is zero). Figure 9.21. Scan From String used to extract a floating point numeric from an input string
Scan From String is also capable of converting string data to more than just numeric data. You can extract strings, paths, enums, Booleans, and time stamps from strings. For example, using the format string %s will extract a "FALSE" or "TRUE" from a string, as a Boolean. Similarly, you can use the format string %d to extract a "0" or "1" from a string, as a Boolean. However, you must be careful with scanning non-numeric data from strings, as the Scan From String function will often stop scanning when it encounters a space (or other whitespace) character. Therefore, it is not quite as flexible as Format Into String is for the inverse operation. Both Format Into String and Scan From String have an Edit Scan String interface that you can use to create the format string. In this dialog box, you can specify format, precision, data type, and width of the converted value. Double click on the function or pop up on it and select Edit Format String to access the Edit Scan String or Edit Format String dialog box (see Figure 9.22). Figure 9.22. Edit Scan String dialogAfter you create the format string and click the Create String button, the dialog box creates the string constant and wires it to the format string input for you. Match Pattern and Regular ExpressionsMatch Pattern is used to look for a given pattern of characters in a string (see Figure 9.23). It searches for and returns a matched substring. Match Pattern looks for the regular expression (or pattern) in string, beginning at offset; if it finds a match, it splits the string into three substrings. If no match is found, the match substring is empty and offset past match is set to -1. Figure 9.23. Match Pattern
A regular expression is a string that uses a special syntax (called regular expression syntax) to describe a set of strings that match a pattern (see Figure 9.24)the syntax (and some useful examples) will be described shortly. Figure 9.24. Match Pattern used to find a pattern in an input string
The Match Pattern function allows you to use some special characters to give you more powerful and flexible searches. Table 9.4 shows you the special characters you can use in the Match Pattern function.
If you have used regular expressions before in other programming languages or with UNIX command-line utilities like grep, you know how powerful and useful regular expressions can be. If you are not familiar with regular expressions, they are a very powerful set of rules for matching and parsing text. Table 9.5 shows you some examples of pattern matching using regular expressions. For more information on regular expression syntax, consult the LabVIEW documentation or a good reference.
The Match Pattern is a relatively fast and powerful way to search for patterns in a string. It does not however, incorporate every aspect of the regular expression syntax. If you need more specialized options to match strings with regular expressions, you can use the Match Regular Expression function. The Match Regular Expression function (see Figure 9.25) incorporates a larger set of options and special characters for string matching, but it is slower than Match Pattern. It uses the Perl Compatible Regular Expressions (PCRE) library, an open source library written by Philip Hazel at the University of Cambridge. Figure 9.25. Match Regular Expression
Because of its additional complexity, Match Regular Expression should only be used if Match Pattern won't work for what you are trying to do. Match Regular Expression is expandable. Resize it vertically (using the Positioning tool to drag the resize handles up or down) to show submatches. If you want to learn more about regular expressionsand there is a vast amount of information availableuse your favorite search engine to find resources on the Web. There are even regular expression libraries (online databases of regular expressions) where you can find some very useful regular expressions. For example, here is a regular expression (we found on the Web) that will match an email address:
Figure 9.26 shows this regular expression, in action. As you can see, regular expressions are an extremely powerful tool! Figure 9.26. Using a regular expression to find email addresses
|