4

I want to retrieve the string between |2~ and |

What is the regex sequence to make this work?

Supposedly I have |2~21381030213jafjs231|adfafafasdfas And I am using

StringTokenizer test = new StringTokenizer(string, delim); 

I tried StringTokenizer(string, "//|/2/~%s/|") // doesn't work.

but it doesn't work.

Can you also elaborate how regex work (like how regex sequence works)? Or good link to start looking into.

Thank you

2 Answers 2

3

If you need the Regex, .*~(.*)\| will do. And here is a Regex 101 to prove it. Now, to explain the Regex, it's pretty simple:

  • .* tells the engine to match all characters;
  • ~ then tells the engine to stop when it finds a ~;
  • (.*) then tells the engine to match all characters and group them as a match;
  • \| then finally tells the engine to match the | which will stop the grouping.

But to learn Regex, have a look at this site, it's one of the best on the web.

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

5 Comments

@user2698142, I did explain it as best I could. In order. But for you to get a better understanding have a look at the link I provided at the end of the answer. You need to learn Regex, and that's way outside the scope of this question.
~ then tells the engine to stop when it finds a ~; Given that you used a greedy quantifier for your ., it will stop when it finds a ~, but it searches from the back first.
@Cruncher, you are correct. It could be modified to be .*?~ instead.
i have to say though that the website that teaches regex makes it even more confusing.
@JungJoo, it's actually one of the best resources on the web, but Regex isn't a simple language. Do some more searching, and if you find a better resource let me know.
3

StringTokenizer does not support regex. You need to use the Pattern & Matcher classes

String s = "|2~21381030213jafjs231|adfafafasdfas"; Matcher m = Pattern.compile("\\|\\d~([^|]+)\\|").matcher(s); if ( m.find() ) { System.out.println(m.group(1)); } 

6 Comments

oh really? i thought delimiter and regex were same thing
@user2698142, even if it doesn't support Regex, a Regex is what you need for this task. Splitting it on a token wouldn't be good enough. Or it would at least require some interesting sub stringing.
you think its easier to pass it into scanner? and use findinline(regex)?
findInLine will give you the full match not the group contents you have to modify the regex to use non look ahead+behind
really nice answer, can you explain what you did for the pattern? i dont understand ([^}]+). thanks.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.