1

I have a string like this val str = "luckycore.util.BigNum("0")", the first five character of the string e.g lucky are dynamic and can change while rest of the string is fixed, what I was trying was to get output as luckyInt(0) i.e replace all charaters except dynamic one with Int and also remove quotes around 0, I tried using replace and substring methods and its working fine for me, but I want to get this output using regex, I tried but nothing is working, someone please help.

one more case is:

 input string = richardcore.util.BigNum("0")

 output string = richardInt(0)
10
  • "tried using replace and substring methods and its working fine for me, but I want to get this output using regex" <- Why? If you have a working solution use that. There is nothing magic about regular expressions and they are imho overused. Commented Jun 13, 2017 at 15:34
  • Find ^(.{5}).*?"(\d+)".*$ replace $1Int($2) Commented Jun 13, 2017 at 15:40
  • @both solution not woking Commented Jun 13, 2017 at 15:56
  • 1
    Oh, you want this ^(.*?)core.util.BigNum.*?"(\d+)".*$ Commented Jun 13, 2017 at 16:20
  • let me try it out first.. Commented Jun 13, 2017 at 16:24

3 Answers 3

1

There is no need for Regex, you can simply do it:

val str = """luckycore.util.BigNum("0")"""
val l = str.replace(str.substring(str.indexOf("core"),str.indexOf("(")).trim,"Int").replace("\"","")
Sign up to request clarification or add additional context in comments.

1 Comment

as long as the dynamic part can never contain "core". Maybe lastIndexOf?
1

You can try using the Regex ([a-z]{5})(core\.util\.BigNum\(\"0\"\)) and then replace the String with the first captured Group.

You can play with this regex at https://regex101.com/r/1DhgLo/1

I am not familiar with Scala or Java. But the Regex should be pretty much remain the same.

6 Comments

not wokring, I wanted a working solution, u just answered by asking the same question
Do you think Regex is not working or the java/scala code is not working ?
@SayimKhan - Which input values is it failing ?
same as typed in above question
If you open the link, it will show you both the input you specified in questions to be matching. I am not sure what is not working for you..
|
0

If I understand your requirement correctly, there is no need for Regex. You can define a simple function (in Scala) as follows:

// Replace substring trailing after the first n characters 
def replaceTail(s: String, n: Int, tail: String) = {
  s.substring(0, n) + tail
}

val str1 = """luckycore.util.BigNum("0")"""
replaceTail(str1, 5, "Int(0)")
res1: String = luckyInt(0)

val str2 = """richardcore.util.BigNum("0")"""
replaceTail(str2, 7, "Int(0)")
res2: String = richardInt(0)

1 Comment

If the OP always knows what the dynamic part is (or just its length), it is of course trivial. So it's a safe assumption the dynamic part is not known, so something slightly more flexible is needed.

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.