siloan
siloan

Reputation: 89

Regex match string conditions

Since i'm not so good at regex how can i match some conditions in a string, StaticString_1number:1number:more than 1number.

Example:

string_3:0:12344555 - Match 
string_s:0:12344555 - No match 
string_3:s:12344555 - No match 
string_3:0:123s4555 - No match

Thanks.

Upvotes: 1

Views: 128

Answers (5)

Santosh Panda
Santosh Panda

Reputation: 7341

This Regex would solve your problem:

^[a-zA-Z]+_[\d]{1}:[\d]{1}:[\d]+$

You can check this link for verification: http://regexr.com?34uj5

Upvotes: 2

Sri
Sri

Reputation: 41

This may help : ^[a-zA-Z]*_[0-9]:[0-9]:[0-9]*$

Upvotes: 0

user1019830
user1019830

Reputation:

If I understand your pattern StaticString_1number:1number:more than 1number correctly your regex to match against such strings could look like the following:

'^[a-zA-Z]+_[0-9]:[0-9]:[0-9]+$'

or if your environment support character classes:

'^\w+_\d:\d:\d+$'

Upvotes: 1

olly_uk
olly_uk

Reputation: 11865

If the initial String can only have characters a-z then the following should work :

[a-z A-Z]+_\d:\d:\d+

this will match any number of letters up to an underscore then look for single digit before and after colon and multiple digits after second colon.

but you should really have an attempt your self. if in python you could try re-try or in javascript regexpal and try out your regex patterns there first.

Upvotes: 0

sp00m
sp00m

Reputation: 48807

This should suit your needs:

^[^_]+_\d:\d:\d+$

Demo

Upvotes: 1

Related Questions