Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
989 views
in Technique[技术] by (71.8m points)

regex - RegExp in Zapier not returning any values

I'm trying to extract some values from discord message in Zapier. The content of the message should look somewhat like this (almost like YAML):

channel: <#1234567890123456789>
as: Bot nicname
image: http://example.com/
content:
Hello, world!

Where image and as fields are optional.

I have created 2 regular expressions to fulfill this task:

Python:

import re
r = re.compile(r"(?:channel:)? ?<#(?P<channel>d+)>
+(?:as: ?(?P<as>.+)
+)?(:?image: ?(?P<image>.+)
+)?content:
*(?P<content>[sS]+)")

JS:

let r = /(?:channel:)? ?<#(?<channel>d+)>
+(?:as: ?(?<as>.+)
+)?(?:image: ?(?<image>.+)
+)?content:
*(?<content>[sS]+)/;

What I tried:

Testing regexes in regexr and pythex. Both work fine for me.

Then I entered them into Zapier:

output from the python code:

    groups: null
    id: <ID>
    runtime_meta:
    memory_used_mb: 57
    duration_ms: 3
    logs:
    1. re.compile('(?:channel:)? ?<#(?P<channel>\d+)>\n+(?:as: ?(?P<nick>.+)\n+)?(:?image: ?(?P<image>.+)\n+)?content:\n*(?P<content>[\s\S]+)')
    2. 'channel: <#1234567890123456> 
as: bot nickname
content:
Hello, world!'
    3. None

(and later in the Run JavaScript with similar result)

What works (somewhat):

When trying to debug it i removed image part of the regexp (in the Text->Extract expression):

(?:channel:)? ?<#(?P<channel>d+)>
+(?:as: ?(?P<as>.+)
+)?content:
*(?P<content>[sS]+)

With the input:

channel: <#1234567890123456>
as: INFO
content:
Hello, world!

And the result was as expected:

output:
0: 1234567890123456
1: INFO
2: Hello, world!
_end: 68
_matched: true
_start: 0
as: INFO
channel: 1234567890123456
content: Hello, world!

Any help with getting it to work is appreciated :)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can match 0 or more whitespaces except newline using [^S ]*. Use s* to match 0+ times any whitespace char including a newline.

Using ? optionally matches a single space.

Depending on the string that you want to match, you can determine to match spaces on the same line, or if it is also accepted to cross newlines.

You might update the pattern to:

(?:channel:)?[^S
]*<#(?P<channel>d+)>s*
(?:as:[^S
]*(?P<as>.+)
+)?(:?image:[^S
]*(?P<image>.+)
+)?content:
*(?P<content>[sS]+)

Regex demo


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...