0

I am new to Java. Please help me with a Java regex to match a pattern and retrieve the value. I need to match the pattern bellow:

\# someproperty=somevalue // this is a new property

\#someproperty=somevalue // this is a new property

I have to match the above patterns (which may contains spaces) and I need to retrieve "someproperty" and "somevalue".

I tried with the pattern below, but it just matches only someproperty=somevalue , without "#" at the beginning. Please help me out.

Pattern propertyKeyPattern = Pattern.compile("^\\s*(\\S+?)\\s*=.*?");
dda
  • 6,030
  • 2
  • 25
  • 34
Lolly
  • 34,250
  • 42
  • 115
  • 150

2 Answers2

2

If you want to match the whole string and find patterns, such as "\# someproperty =some value". Try regular Expression

^\\#\s*(\S+?)\s*=(.*)$

as Java string, it is

"^\\\\#\\s*(\\S+?)\\s*=(.*)$"

The match result for string \# someproperty = a some value is

matches() = Yes

find()    = Yes

group(0)  = \# someproperty = a some value

group(1)  = someproperty

group(2)  = a some value
Kleenestar
  • 769
  • 4
  • 4
0

String a=yourString.replaceAll("[^\w\s]"," "); By using this you will get "someproperty" and "somevalue" string then u can check it. For more post your question clearly.

Kanagaraj M
  • 956
  • 8
  • 18