Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

bash string check 1

Status
Not open for further replies.

w5000

Technical User
Joined
Nov 24, 2010
Messages
223
Location
PL

Hello,
why test returns "clean" what is marked in red in belongs strings comparison?

Code:
# d=274y82heE
# [[ $d =~ [^[:alnum:]] ]] && echo "not clean $d" || echo "clean $d"
[COLOR=#4E9A06]clean 274y82heE[/color]
# d=274y82he^E
# [[ $d =~ [^[:alnum:]] ]] && echo "not clean $d" || echo "clean $d"
[COLOR=#4E9A06]not clean 274y82he^E[/color]

# d=/one/ssfdsf/274y82he^E/end/end
# [[ $d =~ /one/ssfdsf/[^[:alnum:]]/end/end ]] && echo "not clean $d" || echo "clean $d"
[COLOR=#EF2929]clean /one/ssfdsf/274y82he^E/end/end[/color]
# [[ $d =~ /one/ssfdsf/[[:alnum:]]/end/end ]] && echo "not clean $d" || echo "clean $d"
[COLOR=#EF2929]clean /one/ssfdsf/274y82he^E/end/end[/color]
# [[ $d =~ ^/one/ssfdsf/[[:alnum:]]/end/end ]] && echo "not clean $d" || echo "clean $d"
[COLOR=#EF2929]clean /one/ssfdsf/274y82he^E/end/end[/color]
# [[ $d =~ ^/one/ssfdsf/[[:alnum:]]/end/end$ ]] && echo "not clean $d" || echo "clean $d"
[COLOR=#EF2929]clean /one/ssfdsf/274y82he^E/end/end[/color]
 
Hi

[ul]
[li][tt][[:alnum:]][/tt] - one alphanumeric character[/li]
[li][tt][^[:alnum:]][/tt] - one non-alphanumeric character[/li]
[/ul]
In that place you have 8 alphanumeric character, then 1 non-alphanumeric character, then 1 alphanumeric character.

Not sure what is your goal, but try something like this :
Bash:
[[ $d =~ /one/ssfdsf/.*[^[:alnum:]].*/end/end ]]

Feherke.
feherke.ga
 
hello,

ok. but it doesn't return clean when I expect it clean:

Code:
# d=/one/ssfdsf/274y82heE/end/end
# [[ $d =~ ^/one/ssfdsf/[[:alnum:]].*/end/end$ ]] && echo "not clean $d" || echo "clean $d"
not clean /one/ssfdsf/274y82heE/end/end

the goal is to check if there is "alnum" between ^/one/ssfdsf/ and /end/end$
 
Hi

Your current regular expression says :
[ul]
[li]starts with "/one/ssfdsf/"[/li]
[li]then 1 alphanumeric character[/li]
[li]then any number of any character[/li]
[li]ends with "/end/end"[/li]
[/ul]
As your current string looks exactly like that, so the 1st [tt]echo[/tt] gets executed, saying "not clean".

w5000 said:
the goal is to check if there is "alnum" between ^/one/ssfdsf/ and /end/end$
Your current regular expression says in that place has to be 1 alnum followed by anything or nothing.

I think you want this, so in that place to have 1 or more alphanumeric characters :
Bash:
[[ $d =~ ^/one/ssfdsf/[[:alnum:]]+/end/end$ ]] && echo "clean $d" || echo "not clean $d"


Feherke.
feherke.ga
 
thx.
indeed, I was pretty sure I had tested [[:alnum:]]+ in one of my first tries... I must had done some mistake
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top