0

I was curious if you can make regex to match 2nd character of a string with 2 from the back? For 1st and last its pretty easy and straightforward but i was curious if it can be done for any str length? I was playing with it in bash for the last hour and none of my solutions seams to work.

^(.).*\1$ thats the regex I have for 1st and last char it probably needs too be a little edited for this but i have no idea how.

Can you help me with the other one?

examples:
abcsba - match as b(index 1) == b(index -2)
regex - match as e(index 1) == a(index -2)
abba - match 
unix - not matcha as indexOf(n) != indexOf(i)
linux - not match as indexOf(i) != indexOf(u)

2 Answers 2

3

Just discard equal number of character from the beginning and from the end:

$ pat='^.(.).*\1.$'
$ [[ abcsba =~ $pat ]] && echo yes || echo no
yes
$ [[ unix =~ $pat ]] && echo yes || echo no
no

or, generally use {n} (eg. to match 3rd character with 3rd from the end):

pat='^.{2}(.).*\1.{2}$'
$ [[ abcdef =~ $pat ]] && echo yes || echo no
no
$ [[ abccef =~ $pat ]] && echo yes || echo no
yes

In the second example we use single-character-ERE duplication operator {m,n} defined in POSIX for both basic and extended regular expressions (ERE variant is only relevant here though, since =~ operator in bash uses ERE). The {n} form is a special case, equal to {n,n}, meaning repeat the preceding character (or group) exactly n times.

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

8 Comments

When i'm using it to grep words that have the same 2nd and -2 string the same in grep -E '^.(.).*\1.$' it doesn't match all of the words like for example "did" so its like your regex doesnt like mine. When i run mine i have "did" in there and when im using mine and your together it doesnt match mine words.
That's because with ^.(.).*\1.$ we said that we want second character repeated as second to last. So, abba will match as well as adida.
Yes it works fine but it doesnt match words from previous grep. How can I join them so it will match both first last and 2nd and 2nd back?
You can combine them with alternation operator |, for example: ^(.).*\1$|^.(.).*\2.$. You need to use \2 in second part to match the second captured group.
That's simple, too. Try: ^(.)(.).*\2\1$. But please don't expand your original question in comments -- better ask a new one (and close this one by upvoting/accepting an answer). This is quite unreadable and not of much benefit for the next user that comes along.
|
0

As i understood you want to extract the characters . You can do it by awk command

echo "praveen" | awk '{print substr($1,1,2)}' pr 01HW497089:tmp Controller$

Here i am extracting column1 value from character 1 to character 3

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.