0

I'm in Java and have a string that will always be in this format:

;<a href="#" onClick="return CCL(this,'#461610734')" cs="c6"><b>gerg(1314)</b><br> &nbsp;&nbsp;(KC)</a><br>

This number 461610734 will change and may be any length.. I'd like to pick that number out and use it. As you can see the number is next to a ' (the first one working backwards) and a hash # (again, the first one working backwards).

I can find the numbers after the hash by using ([^\#]+$) and I can find up to the last ' by using ([^\']+$) (but this would be on the wrong side of the '...)

I'm lost... Anyone know how to join these two together and nudge the ' along one to the left to just get the numbers?

2
  • 1
    Would something like (?<='#)\d+ as a Java string (?<='#)\\d+ help you? Using a lookbehind. Commented Mar 19, 2014 at 0:46
  • 1
    This one works perfectly! Thanks! my idea was so far away! hah Commented Mar 19, 2014 at 0:49

2 Answers 2

1

Actually, I believe that you could simply extract "the digits that immediately follow a #".

You could then use the following regex: (?<=#)\d+.


On the other hand, if you really want to specify that your digits are following a # and followed by a ', you could (should?) make use of the look-arounds.
The following regex should be what you're looking for:

(?<=#)\d+(?=')

You can see it live by clicking this link.

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

1 Comment

this would work perfectly (i should have mentioned that there were a few numbers missing from the beginning of the string which i didnt inclide in the string example... hence the request for backwards.) Thanks though!
0

Try this:

String str = ";<a href=\"#\" onClick=\"return CCL(this,'#461610734')\" cs=\"c6\"><b>gerg(1314)</b><br> &nbsp;&nbsp;(KC)</a><br>";
Pattern pattern = Pattern.compile("onClick=\"return CCL\\(this,'#([0-9]+)'");
Matcher matcher = pattern.matcher(str);
if (matcher.find()) {
    System.out.println(matcher.group(1)); // Prints 461610734
}

Comments

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.