0

I'm trying to match a specific item in a string using a flavor of JavaScript regex that allows lookbehinds, but I believe I am messing up the lookarounds, possibly in the nesting of them or I'm using the wrong lookarounds altogether.

Here is the regex I have put together so far:

(?<=<a class="" href="/voice/m/contact/(?=\S*?))([\s\S]*?)(?=</a>)

Here is a snippet of the string I am trying to pull the match from:

<b>

<a class="" href="/voice/m/contact/dhbte4tgsbhh65u6">Name</a>
</b>

In the above example, the regex is matching dhbte4tgsbhh65u6">Name when I want it to match Name. I somehow need it to not include the dhbte4tgsbhh65u6"> which is being matched by (?=\S*?).

How can I fix my regex to do what I need?

HeatherLeigh
  • 71
  • 2
  • 10

2 Answers2

0
(?!.*?\/voice\/m\/contact\/\w+[^a-zA-Z0-9]+[a-zA-Z\s\d]*.*)(?<=>)[a-zA-Z\s\d]*

Try this. See demo.

http://regex101.com/r/iX5xR2/22

vks
  • 67,027
  • 10
  • 91
  • 124
  • That would work if the snippet I included were the only thing I needed to match from, however it is just part of a longer string that includes multiple `` tags, so unfortunately, that regex will not work. I need to ensure it matches only the characters following `/voice/m/contact/dhbte4tgsbhh65u6">` like my example. The trouble, of course, is that the sequence of letters/numbers after /contact/ varies. – HeatherLeigh Sep 08 '14 at 09:28
  • @HeatherLeigh try now.changes made. – vks Sep 08 '14 at 09:32
  • That would be great, except sometimes the `Name` is more than one word, thus it has a space. When I test that regex with more than one word in the `Name` area, it doesn't work. Also, is there any way to do this without groups? I'm using the regex to search for what I need and put it in a variable in an Android program called Tasker and I believe the last time I tried using a group it didn't work so I was avoiding them. – HeatherLeigh Sep 08 '14 at 09:45
  • @HeatherLeigh try now.it will except few words including intergers.removing groups would be though. – vks Sep 08 '14 at 09:54
  • Darn, it's perfect except I just tested it in my application and indeed, groups are not supported. It spit out the entire `/voice/m/contact/dhbte4tgsbhh65u6">Name`. Are there any other methods to match just `Name` that you can think of? – HeatherLeigh Sep 08 '14 at 10:00
  • @HeatherLeigh does it support lookbehinds? – vks Sep 08 '14 at 10:15
  • Lookbehinds yes, but not variable lookbehinds. – HeatherLeigh Sep 08 '14 at 15:53
  • @HeatherLeigh its not actually correect.it will work for other tags as well. – vks Sep 08 '14 at 16:04
  • Marking yours as the answer because you tried very hard to meet my needs and I'll be using a variation of yours in my final solution, I think! – HeatherLeigh Sep 09 '14 at 04:55
0

Lookahead will do zero width match it won't match any character. So when you use this regex (?<=<a class="" href="/voice/m/contact/(?=\S*?)) it first checks that the string contact/ is followed by zero or more non-space characters. If yes, then it looks after the contact/ string which was actually located inside <a class="" href="/voice/m/contact/. So this lookbehind,

(?<=<a class="" href="/voice/m/contact/(?=\S*?))

Sets the matching marker just after to the string contact/ and captures all the space and non-space chars ([\s\S]*?) upto the next closing </a> tag.

If your language supports \K then you don't need to use a lookbehind to extract the Name part.

<a class="" href="/voice/m/contact/[^>]*>\K([\s\S]*?)(?=</a>)

DEMO

Avinash Raj
  • 172,303
  • 28
  • 230
  • 274