Hey guys,
I’m trying to extract some string by some conditions from a string and i’m not sure if it’s possible with one regular expression.
This is my string for example:
string bla bla bla sj4i2 text text
What i need to extract is letters and numbers in one word, in that case is “sj4i2”
It’s must contain letters and numbers, but can also contain “-_.$&”
I’m thinking about approach of grab all matches into array and then check them separately…not sure if there’s any shorter way…
@DaveMaxwell
Thank you, this is working only in this order “letters numbers signs” unfortunately i don’t know what order it will be.
Sorry i wasn’t clear about it.
This could be a problem. In order to be able to use regex to match a pattern, knowing would should be matched and what shouldn’t be matched is essential.
I’m assuming
the pattern will never match spaces
there will always be at least one letter and one numeric character
there may also so be certain “special” characters
the order and number of characters will vary
Can you post representative lists of possible correct and incorrect matches? eg.
ad5f, h7gyn, wt42g should match
5dl, kg4 should not match
etc.
everything that includes numbers and letters is correct.
Not correct is number or letters separately.
For example not ok is: 11 22 sss fff ggg 00 - & @
Ok is: 1sd1 2sd03-sd 49s-5
etc…
must begin with 1 or more numbers, must be followed by 1 or more letters, may end with 0 or more mixed
must begin with 1 or more numbers, may be followed by 0 or more mixed, must end with 1 or more letters
must begin with 1 or more letters, must be followed by 1 or more numbers, may end with 0 or more mixed
must begin with 1 or more letters, may be followed by 0 or more mixed, must end with 1 or more numbers
may begin with 0 or more mixed, must be followed by 1 or more numbers, must end with 1 or more letters
may begin with 0 or more mixed, must be followed by 1 or more letters, must end with 1 or more numbers
The “may be mixed” means some sequences will match more than one sub pattern, but without getting into fancy lookbacks / lookaheads I think that should work.
Sorry, but “not working” is not helpful for determining what the problem might be.
Please post some examples of what is being matched that shouldn’t be getting matched or what isn’t getting matched that should be getting matched. Any error message(s) you are getting. A small bit of example code enough so others can see what’s happening. etc.