| Home | Trees | Indices | Help |
|---|
|
|
Here are some common low-level expressions that may be useful in jump-starting parser development:
Parse actions:
Example:
pyparsing_common.number.runTests('''
# any int or real number, returned as the appropriate type
100
-100
+100
3.14159
6.02e23
1e-12
''')
pyparsing_common.fnumber.runTests('''
# any int or real number, returned as float
100
-100
+100
3.14159
6.02e23
1e-12
''')
pyparsing_common.hex_integer.runTests('''
# hex numbers
100
FF
''')
pyparsing_common.fraction.runTests('''
# fractions
1/2
-3/4
''')
pyparsing_common.mixed_integer.runTests('''
# mixed fractions
1
1/2
-3/4
1-3/4
''')
import uuid
pyparsing_common.uuid.setParseAction(tokenMap(uuid.UUID))
pyparsing_common.uuid.runTests('''
# uuid
12345678-1234-5678-1234-567812345678
''')
prints:
# any int or real number, returned as the appropriate type
100
[100]
-100
[-100]
+100
[100]
3.14159
[3.14159]
6.02e23
[6.02e+23]
1e-12
[1e-12]
# any int or real number, returned as float
100
[100.0]
-100
[-100.0]
+100
[100.0]
3.14159
[3.14159]
6.02e23
[6.02e+23]
1e-12
[1e-12]
# hex numbers
100
[256]
FF
[255]
# fractions
1/2
[0.5]
-3/4
[-0.75]
# mixed fractions
1
[1]
1/2
[0.5]
-3/4
[-0.75]
1-3/4
[1.75]
# uuid
12345678-1234-5678-1234-567812345678
[UUID('12345678-1234-5678-1234-567812345678')]
|
|||
|
|||
|
|||
|
|||
|
|||
|
|||
|
|||
|
|||
|
|||
|
|||
integer = integerexpression that parses an unsigned integer, returns an int |
|||
hex_integer = hex integerexpression that parses a hexadecimal integer, returns an int |
|||
signed_integer = signed integerexpression that parses an integer with optional leading sign, returns an int |
|||
fraction = fractionfractional expression of an integer divided by an integer, returns a float |
|||
mixed_integer = fraction or mixed integer-fractionmixed integer of the form 'integer - fraction', with optional leading integer, returns float |
|||
real = real numberexpression that parses a floating point number and returns a float |
|||
sci_real = real number with scientific notationexpression that parses a floating point number with optional scientific notation and returns a float |
|||
number = {real number with scientific notation | real number |any numeric expression, returns the corresponding Python type |
|||
fnumber = fnumberany int or real number, returned as float |
|||
identifier = identifiertypical code identifier (leading alpha or '_', followed by 0 or more alphas, nums, or '_') |
|||
ipv4_address = IPv4 addressIPv4 address (``0.0.0.0 - 255.255.255.255``) |
|||
_ipv6_part = hex_integer
|
|||
_full_ipv6_address = full IPv6 address
|
|||
_short_ipv6_address = short IPv6 address
|
|||
_mixed_ipv6_address = mixed IPv6 address
|
|||
ipv6_address = IPv6 addressIPv6 address (long, short, or mixed form) |
|||
mac_address = MAC addressMAC address xx:xx:xx:xx:xx (may also have '-' or '.' delimiters) |
|||
iso8601_date = ISO8601 dateISO8601 date (``yyyy-mm-dd``) |
|||
iso8601_datetime = ISO8601 datetimeISO8601 datetime (``yyyy-mm-ddThh:mm:ss.s(Z|+-00:00)``) - trailing seconds, milliseconds, and timezone optional; accepts separating ``'T'`` or ``' '`` |
|||
uuid = UUIDUUID (``xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx``) |
|||
_html_stripper = {Suppress:(<any tag>) | Suppress:(</any tag>)}
|
|||
_commasepitem = commaItem
|
|||
comma_separated_list = comma separated listPredefined expression of 1 or more printable words or quoted strings, separated by commas. |
|||
|
|||
Helper to create a parse action for converting parsed date string to Python datetime.date Params -
Example:
date_expr = pyparsing_common.iso8601_date.copy()
date_expr.setParseAction(pyparsing_common.convertToDate())
print(date_expr.parseString("1999-12-31"))
prints: [datetime.date(1999, 12, 31)] |
Helper to create a parse action for converting parsed datetime string to Python datetime.datetime Params -
Example:
dt_expr = pyparsing_common.iso8601_datetime.copy()
dt_expr.setParseAction(pyparsing_common.convertToDatetime())
print(dt_expr.parseString("1999-12-31T23:59:59.999"))
prints: [datetime.datetime(1999, 12, 31, 23, 59, 59, 999000)] |
Parse action to remove HTML tags from web page HTML source Example:
# strip HTML links from normal text
text = '<td>More info at the <a href="https://github.com/pyparsing/pyparsing/wiki">pyparsing</a> wiki page</td>'
td, td_end = makeHTMLTags("TD")
table_text = td + SkipTo(td_end).setParseAction(pyparsing_common.stripHTMLTags)("body") + td_end
print(table_text.parseString(text).body)
Prints: More info at the pyparsing wiki page |
|
|||
numberany numeric expression, returns the corresponding Python type
|
| Home | Trees | Indices | Help |
|---|
| Generated by Epydoc 3.0.1 on Wed Dec 25 07:03:30 2019 | http://epydoc.sourceforge.net |