Using the regex to get particular value

Hi,
String 1 = 'have a nice day %1"
In the above string, I have %1 Which will be determined based on input string and return the value.
If the input string is “have a nice day Gowtham”,the output will be Gowtham.
Can anyone help with regex to get like this?
Thanks in advance

Do you really need help to construct a regular expression that matches on a statically known prefix and the remainder of the string?

Statically known prefix (.*)

This should be enough of hints to construct the real expression on your own.

Though perhaps you do want fmt.Scan* instead?

1 Like

Hi Norbert,

Thank you
Yes, with regex I always get full string like below.But my requirement is to get only that word which will fit in .*

import (
   "regexp"
)

func main() {

   regex:=`have a nice day (.*)`
   str:=`have a nice day Gowtham`
   var rgx =regexp.MustCompile(regex)
   println(rgx.FindString(str)) //have a nice day Gowtham
   println(rgx.MatchString(str))

}

But I want only Gowtham and not “have a nice day Gowtham”
I’m not able to see any inbuilt method which will return me such results.

we have in Java as below,

if (matcher.find()) {
            System.out.println("Yes found : " + matcher.group(1)); // it will print only Gowtham
        }

I have found out to use below code in golang and get the index of 1 for getting the exact value. Is there any other way?

rgx.FindStringSubmatch(str)

So you do not need help with building the regular expression, but with using it?

You should be able to use regexp.(*Regexp).FindString() or regexp.(*Regexp).FindStringSubmatch().

1 Like

Yes.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.