2

In java, I have this URL as a string:

window.location.href = 
"http://localhost:8080/bladdey/shop/c6c8262a-bfd0-4ea3-aa6e-d466a28f875/hired-3";

I want to create a javascript regular expression to pull out the following string:

c6c8262a-bfd0-4ea3-aa6e-d466a28f875

To find left hand marker for the text, I could use the regex:

window\.location\.href \= \"http\:\/\/localhost:8080\/bladdey\/shop\/

However, I don't know how to get to the text between that and /hired3"

What is the best way to pull out that string from a URL using javascript?

Eric Leschinski
155k96 gold badges423 silver badges337 bronze badges
asked Jul 29, 2013 at 14:53
1
  • is the base fixed and the end always like /*? Commented Jul 29, 2013 at 14:55

6 Answers 6

2

You could split the string in tokens and look for a string that has 4 occurrences of -.

Or, if the base is always the same, you could use the following code:

String myString = window.location.href;
myString = myString.substring("http://localhost:8080/bladdey/shop/".Length());
myString = myString.subString(0, myString.indexOf('/'));
Eric Leschinski
155k96 gold badges423 silver badges337 bronze badges
answered Jul 29, 2013 at 14:57

Comments

1

Use a lookahead and a lookbehind,

(?<=http://localhost:8080/bladdey/shop/).+?(?=/hired3)

Check here for more information.

Also, there is no need to escape the : or / characters.

answered Jul 29, 2013 at 14:56

2 Comments

does not compile. I get: Invalid escape sequence (valid ones are \b \t \n \f \r \" \' \ \ )
Sorry, I copied your text for the first part of the regex. Like I said in the last part, there is no need to escape : or /. Edited.
1

You need a regex, and some way to use it...

 String theLocation = "http://localhost:8080/bladdey/shop/c6c8262a-bfd0-4ea3-aa6e-d466a28f8752/hired-3";
 String pattern = "(?</bladdey/shop/).+?(?=/hired3)";
 // Create a Pattern object
 Pattern r = Pattern.compile(pattern);
 // Now create matcher object.
 Matcher m = r.matcher(line);
 if (m.find( )) {
 System.out.println("Found value: " + m.group(0) );
 } else {
 System.out.println("NO MATCH");
 }

note - this will still work when you change the host (it only looks for bladdey/shop/)

answered Jul 29, 2013 at 15:01

2 Comments

does not compile. I get: invalid escape sequence (valid ones are \b \t \n \f \r \" \' \\ )
Ah... I hate it when I get those wrong. Too many differences between different platforms. Since it didn't like my \/ I just took them out - apparently forward slashes are OK, only the backslash needs escaping. I think it should be fixed now.
1

You can use capturing groups to pull out some content of your string. In your case :

 Pattern pattern = Pattern.compile("(http://localhost:8080/bladdey/shop/)(.+)(/hired-3)");
 Matcher matcher = pattern.matcher(string);
 if(matcher.matches()){
 String value = matcher.group(2);
 }
answered Jul 29, 2013 at 15:00

2 Comments

how come you don't need to escape anything there?
@BreakoBreako There is no need to add escape characters in this case. You can try it, it works fine.
1
String param = html.replaceFist("(?s)^.*http://localhost:8080/bladdey/shop/([^/]+)/hired-3.*$", "1ドル");
if (param.equals(html)) {
 throw new IllegalStateException("Not found");
}
UUID uuid = new UUID(param);

In regex:

  • (?s) let the . char wildcard also match newline characters.
  • ^ begin of text
  • $ end of text
  • .* zero or more (*) any (.) characters
  • [^...]+ one or more (+) of characters not (^) being ...

Between the first parentheses substitutes 1ドル.

answered Jul 29, 2013 at 15:05

3 Comments

Thanks but it does not include the hired-3 part.
Actually the regex ([^/]+)/ means any sequence of characters that is not a forward slash, and is followed by a forward slash, is also valid.
@BreakoBreako added forgotten hired-3.
0

Well if you want to pull out GUID from anything:

var regex = /[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{11,12}/i

It should really be {12} but in your url it is malformed and has just 15.5 bytes of information.

answered Jul 29, 2013 at 15:53

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.