简体   繁体   English

或匹配的正则表达式

[英]Regular expression for either or matching

I have a string which could end with either 我有一个字符串可能以

"/"

or 要么

"/?secure=true"

I need to validate it. 我需要验证一下。 I tried this 我试过了

\b(/(/?secure=true)?)\b

but I get no matches found with the regex. 但是我找不到与正则表达式匹配的内容。 However, with 但是,随着

^(/(\$secure=true)?)$

match is found. 找到匹配项。 But this wont help in my case as the string that I want to validate will be prefixed with chars something like "thisismystring/" or "thisismystring/?secure=true" . 但这对我来说没有帮助,因为我要验证的字符串将以char "thisismystring/"例如"thisismystring/""thisismystring/?secure=true"

I want to know how to go about this. 我想知道如何去做。

You need to use the pipe symbol (|) to specify an or: 您需要使用管道符号(|)来指定或:

(/|/\?secure=true)$

You also need to escape the question mark, as here, and should specify the "$" for end of string but not the "^" for start of string. 您还需要转义问号(如此处所示),并且应在字符串的末尾指定“ $”,而在字符串的开头则不指定“ ^”。

Both cases would contain the 两种情况都包含

/

So that should not be in the optional part. 因此,这不应在可选部分中。

You put the optional part in ()? 您将可选部分放在()中了吗?

and escape the question mark with backslash 并用反斜杠转义问号

/(\?secure=true)?

And to indicate that it should end with that, you put $ at the end 为了表明它应该以此结尾,请将$放在最后

/(\?secure=true)?$

可以这样做:

/(\?secure=true)?$

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM