regex()

Read(2550) Label: regex,

Here’s how to use regex() function.

s .regex()

Description:

Match a string with the regular expression.

Syntax:

s.regex(rs,rpls)

Note:

The function matches string s with the regular expression rs, replaces the first matched character with string rpls, and returns the string after replacement.

It returns a sequence consisting of matching strings when parameter rpls is absent, and null when the matching fails.

Parameter:

s

A string

rs

Regular expression

rpls

A string

Option:

@c

Case insensitive

@u

Use Unicode to match

@a

Replace all matching characters

Return value:

Sequence or string

Example:

 

A

 

1

4,23,a,test

 

2

a,D

 

3

W,F

 

4

=A1.regex("(\\d),([0-9]*),([a-z]),([a-z]*)")

5

=A2.regex@c("([a-z]),([a-z])")

With @c option, it is case insensitive

6

=A2.regex("([a-z]),([a-z])")

Return null because they don’t match

7

=A3.regex@u("(\\u0057),(\\u0046)")

[W,F] Use unicode to match

8

=A1.regex("([0-9])","hello")

hello,23,a,test

9

=A1.regex@a("([0-9])","hello")

hello,hellohello,a,test

Related function:

A.regex()

cs.regex()

A .regex()

Description:

Match members in a string sequence with the regular expression.

Syntax:

A.regex(rs,Fi)

If no extracting item is specified in rs, match field Fi of the string with rs based on data type. Then return the new record sequence after record sequence A is filtered. Use the current record to match with rs if omitting Fi

A.regex(rs;Fi,…)

If one or more extracting items are specified in rs, split the string members of sequence A according to them, and return the results as a table sequence whose fields are Fi

Note:

The function matches the members of string type in the sequence A with the regular expression rs. The results will be merged into a table sequence whose fields are Fi for returning.

Parameter:

A

A sequence or a record sequence whose members are strings

rs

Regular expressions. The extracting items are specified sub-regular expressions which are separated from each other by separators and each is surrounded with the parentheses. They will match the fields in sequence. For example, "(.*),(a.*)" are two extracting items separated by a comma.

Fi

Resulting field names of string type

Option:

@c

Case insensitive

@u

Use Unicode to match

Return value:

A table sequence

Example:

 

A

 

1

=demo.query("select NAME,SURNAME from EMPLOYEE")

 

2

=A1.(~.array().concat@c())

 

Convert to a sequence of strings

3

=A2.regex("A.*")

By default, use current record to match with rs if omitting Fi

4

=A2.regex("(V.*),(.*)";name,surname)

Match members of A2 with the regular expressions and return a table sequence

5

=file("D:\\a.txt").import@ts()

6

=A5.(#1).regex@c("(.*),(a.*)";id,name)

Match names that start with a or A

7

=file("D:\\c.txt":"UTF-8").import@ts()

 

8

=A7.(~.array().concat@c())

 

9

=A8.regex@u("(\\u9500\\u552e\\u90e8),(.*)";SalesDep,EmployeeName)

Use Unicode to match the Sales Dep.

10

=A1.regex("V.*",NAME)

Without an extracting item, match the regular expression rs with the string type Fi field

Related function:

s.regex()

cs.regex()

cs .regex()

Description:

Match the string members of in the cursor with the regular expression.

Syntax:

cs.regex(rs,Fi,…)

Note:

The function matches the string members in cursor cs with regular expression rs, returns the original cursor whose fields are Fi. It supports multicursors.

Parameter:

cs

A cursor whose members are strings

rs

Regular expression

Fi

Resulting field name

Option:

@c

Case insensitive

@u

Use Unicode to match

Return value:

The original cursor with new fields

Example:

 

A

 

1

=file("D:\\a.txt").import@ts().(~.array().concat@c())

a.txt is a table sequence whose fields are separated by commas

2

=A1.cursor()

 

3

=A1.cursor()

 

4

=A2.regex("(.*),(A.*)";id,name).fetch()

Match employees whose names start with A

5

=A3.regex@c("(.*),(A.*)";id,name).fetch()

 Match employees whose names start with a or A

6

=file("D:\\c.txt":"UTF-8").import@ts().(~.array().concat@c())

 

7

=A6.cursor()

 

8

=A7.regex@u("(\\u9500\\u552e\\u90e8),(.*)";销售部,员工姓名).fetch()

Use Unicode to match the Sales Dep.

Related functions:

s.regex()

A.regex()