admin管理员组

文章数量:1429783

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
Add a ment  | 

2 Answers 2

Reset to default 3

EDIT 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