admin管理员组文章数量:1023204
I have the following regex:
var re = /^(post|merge|delete) (\/WcfDataService1\.svc\/)(.*) HTTP\/1.1$[\s\S]*?(^{.+\}$)/im
This is used to match various items in a batch request:
--batchitems
Content-Type: application/http
Content-Transfer-Encoding: binary
POST /WcfDataService1.svc/Orders(123) HTTP/1.1
{"OrderID":"x58"}
- First group: the type (POST/MERGE/DELETE)
- Second group: /WcfDataService1.svc/
- Third group: anything which appears after WcfDataService1.svc, in this instance: Orders(123)
- Fourth group: the JSON string
This works great! When I execute re.match(str);
, I am returned an array as follows:
[
'POST',
'/WcfDataService1.svc/',
'Orders(123)',
'{"OrderID":"x58"}'
]
However, I want the JSON string to be optional in my regex, as it will not always be present; e.g.:
--batchitems
Content-Type: application/http
Content-Transfer-Encoding: binary
DELETE /WcfDataService1.svc/Orders(123) HTTP/1.1
// EOL here
But my regex fails here because it's trying to match the curly braces and it can't find them, so the entire regex fails.
How to I make (^{.+\}$)
optional (to appear 0 or 1 times)? I tried (^{.+\}$){0,1}
but does not work.
Any ideas?
See: and
I have the following regex:
var re = /^(post|merge|delete) (\/WcfDataService1\.svc\/)(.*) HTTP\/1.1$[\s\S]*?(^{.+\}$)/im
This is used to match various items in a batch request:
--batchitems
Content-Type: application/http
Content-Transfer-Encoding: binary
POST /WcfDataService1.svc/Orders(123) HTTP/1.1
{"OrderID":"x58"}
- First group: the type (POST/MERGE/DELETE)
- Second group: /WcfDataService1.svc/
- Third group: anything which appears after WcfDataService1.svc, in this instance: Orders(123)
- Fourth group: the JSON string
This works great! When I execute re.match(str);
, I am returned an array as follows:
[
'POST',
'/WcfDataService1.svc/',
'Orders(123)',
'{"OrderID":"x58"}'
]
However, I want the JSON string to be optional in my regex, as it will not always be present; e.g.:
--batchitems
Content-Type: application/http
Content-Transfer-Encoding: binary
DELETE /WcfDataService1.svc/Orders(123) HTTP/1.1
// EOL here
But my regex fails here because it's trying to match the curly braces and it can't find them, so the entire regex fails.
How to I make (^{.+\}$)
optional (to appear 0 or 1 times)? I tried (^{.+\}$){0,1}
but does not work.
Any ideas?
See: https://regex101./r/pT3fG0/2 and https://regex101./r/nO8xZ7/1
Share Improve this question asked Aug 20, 2015 at 16:15 keldarkeldar 6,26211 gold badges54 silver badges84 bronze badges 2-
1
Maybe a bit of a silly suggestion, but you can do the basic splitting with some simple string matching, like
var lines = input.split("\n")
and then apply the regular expression to each line and count the lines etc. – Halcyon Commented Aug 20, 2015 at 16:23 -
Have you tried
(^{.+\}$)?
? Or maybe it would be(^{.+\}$){?}
... I'm no regex expert. – UltraSonja Commented Aug 20, 2015 at 16:44
2 Answers
Reset to default 3EDIT 1
make the json part optional using ?
, as follows:
^(post|merge|delete) (\/WcfDataService1\.svc\/)(.*) HTTP\/1.1$(?:[\s]*?(^{.+\}$))?
DEMO
An alternative option could be:
(^(post|merge|delete)\s(\/WcfDataService1\.svc\/)(.*)(HTTP\/1\.1)([\s]*)($|^{.*}))
This works as you are hoping. You can see it here
I have the following regex:
var re = /^(post|merge|delete) (\/WcfDataService1\.svc\/)(.*) HTTP\/1.1$[\s\S]*?(^{.+\}$)/im
This is used to match various items in a batch request:
--batchitems
Content-Type: application/http
Content-Transfer-Encoding: binary
POST /WcfDataService1.svc/Orders(123) HTTP/1.1
{"OrderID":"x58"}
- First group: the type (POST/MERGE/DELETE)
- Second group: /WcfDataService1.svc/
- Third group: anything which appears after WcfDataService1.svc, in this instance: Orders(123)
- Fourth group: the JSON string
This works great! When I execute re.match(str);
, I am returned an array as follows:
[
'POST',
'/WcfDataService1.svc/',
'Orders(123)',
'{"OrderID":"x58"}'
]
However, I want the JSON string to be optional in my regex, as it will not always be present; e.g.:
--batchitems
Content-Type: application/http
Content-Transfer-Encoding: binary
DELETE /WcfDataService1.svc/Orders(123) HTTP/1.1
// EOL here
But my regex fails here because it's trying to match the curly braces and it can't find them, so the entire regex fails.
How to I make (^{.+\}$)
optional (to appear 0 or 1 times)? I tried (^{.+\}$){0,1}
but does not work.
Any ideas?
See: and
I have the following regex:
var re = /^(post|merge|delete) (\/WcfDataService1\.svc\/)(.*) HTTP\/1.1$[\s\S]*?(^{.+\}$)/im
This is used to match various items in a batch request:
--batchitems
Content-Type: application/http
Content-Transfer-Encoding: binary
POST /WcfDataService1.svc/Orders(123) HTTP/1.1
{"OrderID":"x58"}
- First group: the type (POST/MERGE/DELETE)
- Second group: /WcfDataService1.svc/
- Third group: anything which appears after WcfDataService1.svc, in this instance: Orders(123)
- Fourth group: the JSON string
This works great! When I execute re.match(str);
, I am returned an array as follows:
[
'POST',
'/WcfDataService1.svc/',
'Orders(123)',
'{"OrderID":"x58"}'
]
However, I want the JSON string to be optional in my regex, as it will not always be present; e.g.:
--batchitems
Content-Type: application/http
Content-Transfer-Encoding: binary
DELETE /WcfDataService1.svc/Orders(123) HTTP/1.1
// EOL here
But my regex fails here because it's trying to match the curly braces and it can't find them, so the entire regex fails.
How to I make (^{.+\}$)
optional (to appear 0 or 1 times)? I tried (^{.+\}$){0,1}
but does not work.
Any ideas?
See: https://regex101./r/pT3fG0/2 and https://regex101./r/nO8xZ7/1
Share Improve this question asked Aug 20, 2015 at 16:15 keldarkeldar 6,26211 gold badges54 silver badges84 bronze badges 2-
1
Maybe a bit of a silly suggestion, but you can do the basic splitting with some simple string matching, like
var lines = input.split("\n")
and then apply the regular expression to each line and count the lines etc. – Halcyon Commented Aug 20, 2015 at 16:23 -
Have you tried
(^{.+\}$)?
? Or maybe it would be(^{.+\}$){?}
... I'm no regex expert. – UltraSonja Commented Aug 20, 2015 at 16:44
2 Answers
Reset to default 3EDIT 1
make the json part optional using ?
, as follows:
^(post|merge|delete) (\/WcfDataService1\.svc\/)(.*) HTTP\/1.1$(?:[\s]*?(^{.+\}$))?
DEMO
An alternative option could be:
(^(post|merge|delete)\s(\/WcfDataService1\.svc\/)(.*)(HTTP\/1\.1)([\s]*)($|^{.*}))
This works as you are hoping. You can see it here
本文标签: javascriptRegexmatch 0 or 1 timesStack Overflow
版权声明:本文标题:javascript - Regex - match 0 or 1 times - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745551848a2155667.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论