-2

This is my string

username=john903?fullname=john&smith

This is my regex so far

=([^?]*)

I am trying to capture "john903"

However this currently returns "=john903"

Bricky
  • 2,572
  • 14
  • 30
dark_illusion_909099
  • 1,067
  • 2
  • 21
  • 41

2 Answers2

1

Javascript: (Originally the question was tagged with Javascript)

The regex you have currently captures the value you want, but returns an array. The match without the equal sign is in the second element of the array:

var str = "username=john903?fullname=john&smith".match(/=([^?]*)/)[1] 
//str is now "john903"

Java: (OP commented they are in fact using Java)

String line = "username=john903?fullname=john&smith";
Pattern pattern = Pattern.compile("=([^?]*)");
Matcher matcher = pattern.matcher(line);
System.out.println(matcher.group(1));
Bricky
  • 2,572
  • 14
  • 30
0

You need to utilize positive lookbehind to achieve this. Reference

and here's a fitting regex

(?<==)([^?]*)
Nick Carter
  • 46
  • 1
  • 4