What can be the regex for the next line

I am doing this in groovy.

Entrance:

hip_abc_batch   hip_ndnh_4_abc_copy_from_stgig  abc_copy_from_stgig
hiv_daiv_batch  hip_a_de_copy_from_staging  abc_a_de_copy_from_staging

      

I want to get the last column. basically anything that starts with abc_

.

I tried the following regex (works for the second line, but not the second).

\abc_.*\

      

but it gives me everything after abc_batch

I am looking for a regex that will fetch me everything that starts with abc_

but I cannot use \^abc_.*\

as the whole string does not start with abc _

+2


source to share


4 answers


Try the following:

/\s(abc_.*)$/m

      

Here's a commented version so you can understand how it works:

\s          # match one whitepace character
(abc_.*)    # capture a string that starts with "abc_" and is followed
            # by any character zero or more times
$           # match the end of the string

      



Since the regular expression has a " m

" switch , it will be a multiline expression. This allows you to $

match the end of each line rather than the end of the entire line.

Edit: You don't need to trim the whitespace as the second capture group only contains text. After a cursory scan of this tutorial, I believe this is a way to grab the value of a capture group using Groovy:

matcher = (yourString =~ /\s(abc_.*)$/m)
// this is how you would extract the value from 
// the matcher object
matcher[0][1]

      

+3


source


It sounds like you are looking for "words" (that is, sequences that do not include spaces) that start with abc_

. You may try:

/\babc_.*\b/

      



\b

means (in some regex flavors) "word boundary".

+5


source


Regex buddy (pay) and RegExr (free) can be a big help in learning RegEx if you're interested.

0


source


I think what you are looking for is: \ s (abc_ [a-zA-Z_] *) $

If you are using perl and you are reading all lines on one line, remember to set the "m" option in your regular expression (it means "Treat the line as multiple lines").

Oh, and Regex Coach is your free friend.

0


source







All Articles