-2

Hi don't know much of regular expression and I am trying to get just the digits from the string "glm=4563125@", can someone please help me. The number of digits can vary, so it's not specific the amount of digits that will be there. Thanks.

user3776276
  • 13
  • 1
  • 3
  • Are you trying to get an Integer, a double, etc.? Are you trying to get it as a string or as a number? – jhobbie Jun 25 '14 at 17:23
  • It doesn't matter, I can get it as a String and convert to integer later. I cannot so \d+ because the actual String is longer than what I provided, and it has other digits that I don't want. I just want the digits between glm= and |, glm is not mentioned anywhere else in the complete string. Thanks for the help. – user3776276 Jun 25 '14 at 17:25
  • did you want to get the numbers between glm= and ? – Avinash Raj Jun 25 '14 at 17:27
  • "*it has other digits that I don't want*" you need to provide more informations about input and expected output. We are not mind-readers. Also I don't see any `|` in your current example. – Pshemo Jun 25 '14 at 17:27
  • Sorry I mean between "gml=" and "@". The complete String is something like "glm=4563125@ abcd=efgh @pref= @ alt=?@Altype=wxyz!....(continues)" but no glm= repetations. – user3776276 Jun 25 '14 at 17:33
  • Can `gml=` be part of other "property" for instance `foogml=` or `foo_gml=`? – Pshemo Jun 25 '14 at 17:34
  • No gml= is what the String starts with. – user3776276 Jun 25 '14 at 17:35
  • @user3776276 please edit the question according to your needs. – Avinash Raj Jun 25 '14 at 17:36

3 Answers3

2

Use the Matcher class to match and extract your substring between those delimiters.

String s  = "glm=4563125@ abcd=efgh @pref= @ alt=?@Altype=wxyz! barglm=1234@";
Pattern p = Pattern.compile("\\bglm=(\\d+)@");
Matcher m = p.matcher(s);
while (m.find()) {
  System.out.println(m.group(1)); //=> "4563125"
}

Note: Using a word boundary \b asserts that on one side there is a word character, and on the other side there is not or at the beginning or end of a string if it begins or ends with a word character.

hwnd
  • 69,796
  • 4
  • 95
  • 132
0

As per the comments, to extract digits between "glm=" and "@"

public String extractNumber(String input) {
    Pattern p = Pattern.compile("glm=(\\d+)[@]?");
    Matcher m = p.matcher(input);
    if (m.find()) return m.group(1);
    else return null;
}

Explaination:

The regex glm=(\d+)[@]? inserts a capturing group around the numbers, and searches for numbers inbetween "glm=" and "@", and the question mark makes the "@" character optional (Note: your comments were very misleading about this being optional, so if you don't want this, just remove the question mark)

The Pattern and Matcher are used to search for a matching regex expression in your input string, and the return statement returns the first capturing group found.

I hope this is what you were looking for.

Michael Parker
  • 12,724
  • 5
  • 37
  • 58
  • Hi this works perfectly. And thank you for the explanation, the regex I wrote was similar but I see the error in mine now. Thanks. – user3776276 Jun 25 '14 at 18:08
0

You can use the following regex:

(\d+)

Regular expression visualization

enter image description here

Debuggex Demo

String input = "glm=4563125@";
Pattern p = Pattern.compile("(\\d+)");
Matcher m = p.matcher(input);

if (m.find()) {
    System.out.println(m.group(1)); // Displays 4563125
}
Federico Piazza
  • 30,085
  • 15
  • 87
  • 123