-2

Hi how can i return Grapes from the string below, i want to search through a string and return a text in the middle of the string after four character and discard the rest of the text.

String grapes = "2 x Grapes @Walmart";
Junius L
  • 15,881
  • 6
  • 52
  • 96
  • you want `Grapes` , is at always after 4 characters ? , there may be a case when you buy more grapes and the string becomes 10 X Grapes, i.e. after 5 characters – aelor Apr 22 '14 at 13:51
  • So you want... what? The third word? The middle word? The first word after the first 4 characters? What should `2x Grapes foo` return? – Robin Apr 22 '14 at 13:52
  • Thanks after 4 or 5 characters and discard the remaining text – Junius L Apr 22 '14 at 13:53
  • the third word you mean , right ? – aelor Apr 22 '14 at 13:55
  • 3
    Did you try `grapes.split(" ")[2]`? – Robin Apr 22 '14 at 13:56
  • @Robin i'm completely new android and java can you please clarify the split statement – Junius L Apr 22 '14 at 13:59
  • 1
    You can read more about it here: http://stackoverflow.com/questions/3481828/how-to-split-a-string-in-java . You don't need regex for this task, splitting on whitespace will give you an array with each word in it. – Robin Apr 22 '14 at 14:00

3 Answers3

1

Thanks for helping me guys the code below worked

String grapes = "2 x Grapes @Walmart";
String[] split = grapes.split("\\s+");
String fsplit = split[2];
Junius L
  • 15,881
  • 6
  • 52
  • 96
0

my suggestion will be not to use a regex for this . But just in case you find no other way round, use this :

(\w+\s){3}

you will get the third word in the first backreference. \1 or $1 whichever supports your compiler

demo here : http://regex101.com/r/jB5nN0

aelor
  • 10,892
  • 3
  • 32
  • 48
-1

This may help you:

^[\\d]+\\sx\\s(.*?)\\s+.*?$

Explanation:

Assert position at the beginning of a line (at beginning of the string or after a line break character) «^»
Match a single digit 0..9 «[\d]+»
   Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
Match a single character that is a “whitespace character” (spaces, tabs, and line breaks) «\s»
Match the character “x” literally «x»
Match a single character that is a “whitespace character” (spaces, tabs, and line breaks) «\s»
Match the regular expression below and capture its match into backreference number 1 «(.*?)»
   Match any single character that is not a line break character «.*?»
      Between zero and unlimited times, as few times as possible, expanding as needed (lazy) «*?»
Match a single character that is a “whitespace character” (spaces, tabs, and line breaks) «\s+»
   Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
Match any single character that is not a line break character «.*?»
   Between zero and unlimited times, as few times as possible, expanding as needed (lazy) «*?»
Assert position at the end of a line (at the end of the string or before a line break character) «$»
Pedro Lobito
  • 94,083
  • 31
  • 258
  • 268