-4

I have the string "abc.aspx?sdfsdfds;eter;yid=10". I want my regex to match the 10 part of that string.

I wrote the regex (abc.aspx?*[?;]yid=), but it is not matching my string. The regex abc.aspx?yid=10;sdfsdf matches my string, and I used this instead of *.

Why does the first regex with * not match, but the second one without it does?

I also want ['test'?%] in this clause. i.e before yid i want only ?,%,Test. for single character it works i.e $% but it is working for Test I tried this (abc.aspx.*['Test'?%]yid=) but it consider ' as character rather i want to match whole Test word.

Hemant Malpote
  • 891
  • 13
  • 28
  • 3
    `?` has an special meaning, you probably have to escape it. – fedorqui Apr 21 '15 at 13:30
  • `(\d+)` matches `10`. Can you be more specific about what you want? – Fredrik Pihl Apr 21 '15 at 13:31
  • I'm not sure what you are trying to use * for. Can you have something before yid? If so you'd probabably want to use .* instead of just * since that means any character as many times as necessary. – Nived Apr 21 '15 at 13:31
  • You are missing the point of looking at the string and figuring out the definitive signature that you are looking for in you target. Trivially, for both strings, you can find '10' with `(\d+)`. But what defines the two strings so that you know '10' is the right number? How big is the string? Is there a possibility of more than one number? – dawg Apr 21 '15 at 14:17
  • thanks guys. .* works – Hemant Malpote Apr 21 '15 at 14:28

2 Answers2

1

You have to escape ? and use .* instead of * :

abc.aspx\?.*[?;]?yid=(\d+)

Demo and Explanation

karthik manchala
  • 13,492
  • 1
  • 31
  • 55
1

Escape the question mark for a literal question mark, and if yid can appear immediately after the question mark you need to make the entire intervening input optional:

abc.aspx\?(.*;)?yid=(\d+)

Your target is in group 1.

See live demo working with both of your sample inputs.

Bohemian
  • 412,405
  • 93
  • 575
  • 722