Regular expressions: (mysql) REGEXP

 

Retrieving column prod_name contain text 1000 so that the line

SELECT prod_name FROM  products WHERE prod_name REGEXP ‘1000’

ORDER BY prod_name

 

Why trouble using regular expressions? In the above example, the regular expression does not bring many benefits (performance may also be reduced),

Consider the following example:

SELECT prod_name FROM  products WHERE prod_name REGEXP ‘.000’

ORDER BY prod_name

 

An OR match

 

SELECT prod_name FROM  products WHERE prod_name REGEXP ‘1000|2000’

ORDER BY prod_name

 

 

SELECT prod_name FROM  products WHERE prod_name REGEXP ‘[123] ton’

ORDER BY prod_name

Above the regular expression [123] ton of [1 | 2 | 3] ton Acronym

 

Matching range

Match 0-9 e.g.

[0123456789] simplifies [0-9] [1-3] [6-9] [az] matches any letter

 

Matching special characters

SELECT prod_name FROM  products WHERE prod_name REGEXP ‘\\.’

ORDER BY prod_name

In order to match a special character, you must use \\ as the lead. \\ - Find -, \\ represents a look. .

Blank metacharacters

 

\\ f   feed  \\ n newline   \\ r   carriage  \\ t tab  \\ v vertical tab

\\\ match backslash

 

Matching character class

[: Alnum:] any letters and numbers with [a-zA-Z0-9]

[: Alpha:] Any character (with [a-zAZ])

[: Blank:] and tabs with spaces [\\ t]

[: Cntrl:] ASC || control characters (ASC || 0 to 31 and 127)

[: Digit:] with an arbitrary number ([0-9])

[: Lower:] any lowercase letter [az]

[: Print:] any printable character

[: Punct:] neither [: alnum:] not in [: cntrl:] is any character 

[: Space:] any blank characters including spaces

[: Upper:] any capital letters

[: Xdigit:] any hexadecimal digit with [a-fA-F0-9]

 

 

Examples of the plurality of matching

* 0 or more matches

+ One or more matches (is equal to {1})

? 0 or a match (equal to {0,1})

{N} matches the specified number

{N,} is not less than the specified number of matches

{N, m} matching number range (m 255)

 

 

Here are a few examples:

Input:

SELECT prod_name FROM products WHERE pro_name GEGXP ‘\\(0-9) sticks?\\)’

Output prod_name

           TNT (1 stick)

           TNT (5 stick)

 

Regular expression \\ (0-9) sticks? \\) need to explain. \\ (match (

[0-9] matches any number  

sticks? match sticks and stick (after the s? s optional so it matches any character in front of a 0 or 1 occurrences)

\\) match) did not pay attention to? Match sticks and stick will be very difficult

 

The following is another example of this we intend to match the four digits together

SELECT prod_name FROM products WHERE pro_name GEGXP ‘[[:digit:]]{4}’

 

{4} Request its preceding character (an arbitrary number) occurrence of any four-digit number 4 connected together so that matching

 

The above regular expression can also write: [0-9] [0-9] [0-9] [0-9]

 

The above summary is to match a string anywhere in the text. In order to match the text of a particular position, it is necessary to use locator

^ The text begins

$ End of Text

Beginning of a word [] [:: <]

End of a word [] [::>]

 

For example, you want to find a number (including decimal point start number) of all products began, how do?

Simple search [0-9] or \\. [: Digit:] \\ because it will not find a match anywhere in the text to match.

The solution is to use locator ^

SELECT prod_name FROM products WHERE pro_name GEGXP ‘^[0-9\\.]’

 

 

 

 

 

Splicing field

May be used Concat () function is used

 

Example:

SELECT Concat(vend_name,’(’,vend_country,’)’) FROM vendors ORDER BY vend_name;

 

Concat () splice sequence, i.e. the plurality of strings are connected to form a long string

 

 

Delete unnecessary data with Airbus

SELECT Concat(vend_name,’(’,RTrim(vend_country),’)’) FROM vendors ORDER BY vend_name;

RTRIM () function value the right to remove all spaces

 

Perform arithmetic operations

 

Operators operator Mysql

+ Plus

- Less

* Multiplication

/ Division

 

Entry

SELECT prod_id,quantity,item_price,quantity*item_price AS expanded_price

FROM orderitems WHERE order_num = 2005

 

 

Using a data processing function

Most of the functions implementation supports the following types of functions

Handling text strings (e.g., deleted or filled, is converted to uppercase or lowercase);

Performing arithmetic operations on numerical data (e.g., returns the absolute value, for algebra);

For processing date values, and take these values ​​from the specific component (e.g., return two dates, validity date of detection) the date and time functions;

Return specific information DBMS being used (such as return with login information, check the version details) of system function

 

E.g. Rtrim () remove the right space right column values

 

Upper () function

Entry

SELECT vend_name,Upper(vend_name) AS vend_name_upcase FROM vendors ORDER BY vend_name

 

Upper () converts text to uppercase.

 

 

Commonly used text processing functions

Left () returns the character to the left

Length () returns the length of the string

Locate () to find out a string of sub-string

The Lower () the string to lowercase

LTRIM () remove the spaces left string

Right () returns the string on the right

RTRIM () string right spaces removed

SOUNDEX () Returns the string value SOUNDEX

SubString () returns the string of characters

The Upper () to convert the string to uppercase

 

Hereby explain SOUNDEX any text is a description of an alphanumeric string into its speech mode algorithm expressed.

original

SELECT cust_name,cust_contact FROM customers WHERE cust_contact = ‘Y.Lie’;

The following describes the use Soundex () function to search, it matches the contact name pronunciation is similar to all of Y.Lie

SELECT cust_name,cust_contact FROM customers WHERE Soudex(cust_contact) = Soudex(‘Y.Lie’);

 

 

 

Date Functions

The date and time used handlers

 

Numerical handler

 

 

Aggregate Functions

Guess you like

Origin www.cnblogs.com/mzdljgz/p/11387784.html