0

using regex to get a string between 2 strings and can't figure out how to ignore the start and end strings.

"imgurl=(.*?)&amp"  

this expression working fine except i need to ignore imgurl= and &amp?

by matching the following string:

imgurl=mytext&amp

the result i got is like

imgurl=mytext&amp

it has to be

mytext
3
  • The mytext part will be in captured group 1 (indicated by the first set of brackets in the regex). What language/framework are you using for the regex? Often \1 or $1 refer to whatever's matched by the first set of brackets. Commented Nov 24, 2012 at 10:55
  • Telling us what language you're programming in might help in perl you are able to use $1 after matching something like that in php you have to add another variable at the end of the regexp_match and use the [1] index of that variable to see the correct result Commented Nov 24, 2012 at 10:55
  • @mathematical.coffee i am using VB.NET Commented Nov 24, 2012 at 10:56

1 Answer 1

2

You haven't specified the language, but be sure you are checking the appropriate match group...

>>> msg = "imgurl=mytext&amp"
>>> import re
>>> foo = re.search("imgurl=(.*?)&amp", msg)
>>> foo.group(1)
'mytext'
>>> foo.group(0)
'imgurl=mytext&amp'

Obviously, group 1 is what you're looking for...

EDIT

For vb.net code..

Dim regex As Regex = New Regex("imgurl=(.*?)&amp")
Dim match As Match = regex.Match("imgurl=mytext&amp")

Assuming you get the match, you need the value from match.Groups(1).Value

Sign up to request clarification or add additional context in comments.

1 Comment

For future readers: if your text spans across mutliple lines (contains newline symbols), you need to use RegexOptions.Singleline flag when compiling the Regex object.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.