質問

I have to replace the following in a NSURL:
a_token=lksjadfkj%2gf98273984
with
a_token=new_token

a token can be in the follwing forms:

a_token=989asaofiusaodifusa9f789asdofu&lat=43.3
a_token=lksjadfkj%2gf98273984

So it either ends with & or end line/nothing.

How could I write the regex expression for it?

Thanks!

役に立ちましたか?

解決

You could try:

[stringWithURL stringByReplacingOccurrencesOfRegex:@"(?<=a_token=)[^&]*"
                                        withString:@"new_token"];

I haven't tested it. Basically, the regex uses a look-behind assertion to match a_token=. The look-behind is not included in the text that is matched. Then, the regex matches "zero or more characters that aren't &".

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top