Marco Shaw
Marco Shaw

Reputation: 1118

.NET regex with quote and space

I'm trying to create a regex to match this:

/tags/ud617/?sort=active&page=2" >2

So basically, "[number]" is the only dynamic part:

/tags/ud617/?sort=active&page=[number]" >[number]

The closest I've been able to get (in PowerShell) is:

[regex]::matches('/tags/ud617/?sort=active&page=2" >2
','/tags/ud617/\?sort=active&page=[0-9]+')

But this doesn't provide me with a full match of the dynamic string.

Ultimately, I'll be creating a capture group:

/tags/ud617/?sort=active&page=([number])

Upvotes: 0

Views: 72

Answers (2)

Dean Taylor
Dean Taylor

Reputation: 41991

[regex]::matches('/tags/ud617/?sort=active&page=3000 >2','/tags/ud617/\?sort=active&page=(\d+) >(\d+)')

Outputs:

Groups   : {/tags/ud617/?sort=active&page=3000 >2, 3000, 2}
Success  : True
Captures : {/tags/ud617/?sort=active&page=3000 >2}
Index    : 0
Length   : 41
Value    : /tags/ud617/?sort=active&page=3000 >2

This captures the page value and the number after the greater than i.e. 2

Upvotes: 1

mjolinor
mjolinor

Reputation: 68273

Seems easy enough:

 $regex = '/tags/ud617/\?sort=active&page=(\d+)"\s>2'

'/tags/ud617/?sort=active&page=2" >2' -match $regex > $nul

$matches[1]

2

Upvotes: 1

Related Questions