0

Tried to split and find out what regex pattern can be done for my case , but didn't find solution. I have following string

param1=234&param2=5||param3>3&param4=N/A (04)&param5=some valued&param6=this good - new value&param7=A (newparam)

My steps

1. string.replaceAll("&|\\|\\|", ",");
2. string.split("[\\w,]")

if put such string

param1=234&param2=5||param3>3

then it split up to to values:

param1
234
param2
5
param3
3

But when i have whitespaces and "/" and "(" and "-" then it's divided to each value. What a regex pattern should be used then in order to get following output?

param1
234
param2
5
param3
3
param4
N/A (04)
param5
some valued
param6
this good - new value
param7
A (newparam)

I do it for inserting them after this as key=value into map. May be other way exists to approach but for now if i have separated array of values then i can insert them into a map.

So, for now I need only a regex how to split it. Thanks in advance

4
  • 2
    Split on [=&>]|\\|\\| Commented Feb 16, 2021 at 13:18
  • 1
    Use s.split("\\|{2}|[&=<>]"), see demo. Commented Feb 16, 2021 at 13:20
  • Wow gus i tried to resolve for about to find out what pattern and you wrote here the answer. I need to learn this regex from the beginning. And your answers should be in main answers. Need to vote for you guys. Thanks a lot!! Commented Feb 16, 2021 at 13:20
  • Both of answers are good!! you saved me guys!! Commented Feb 16, 2021 at 13:22

2 Answers 2

3

You can apply this regex in your .split()

[=&>]|\\|\\|
  • [=&>] - find an equal sign or ampersand or >
  • | - or
  • \\|\\| - find double pipes

[=&>] is equivalent to =|&|> so you could also use =|&|>|\\|\\| if it's more legible to you.

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

8 Comments

And what about if i want to add also >= -- equal or greater? param1=234&param2=5||param3>3param4>=5
@master17 since it's two consecutive chars then add it with a pipe at the end like |>=. Ditto for |<=. If you're just adding an individual char then you can throw it inside of the char set braces []
you mean like this : [=&><]||\|\||<=|>=
no its not working for expression with >= and <=
@master17 I took a better look at [=&><]||\|\||<=|>= and it looks wrong. For java you need to escape the escape \\|. Not sure why you made that mistake...
|
1

If you want to create a key-value mapping:

  1. Split on &|\|\|, obtaining strings as param1=234, param2=5 and param3>3.
  2. For each string split on [>=] for obtaining the single key-value pair.

Comments

Your Answer

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.