dsjuimtq920056 2013-05-11 12:42
浏览 135
已采纳

具有多个可选结尾的正则表达式

I have the following URIs:

  • /controller/method/arg1/arg2
  • /controller/method/arg1/arg2/ <- tailing slash
  • /controller/method/arg1/arg2?param=1&param=2 <- tailing query string

I need a regular expression to extract always /controller/method/arg1/arg2, while keeping in mind that it can end with /, ? or ? + some characters.

This:

^\/.*\??

is not working for me, as it is taking the chars before ?.

I don't care if the resulting string includes / or ? at the end, I can trim it.

  • 写回答

1条回答 默认 最新

  • dongyi0114 2013-05-11 12:54
    关注

    I'm sure one can think of more fancy regex's, but as far as I can see, the simplest possible matching regex would be;

    ^[^?]*
    

    It cuts everything after ? off, and the other matches are - as you say - easily handled by trimming.

    A ReFiddle to test with.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算