1

I would like to use regular expression to represent a pattern with the following type of characteristics

1) It includes a continuous sequence of digit values. The length of this sequence it at least 1

2) There has at least one non-digit characters either before the first digit value of the above sequence, or after the last digit value of the above sequence, or both before and end. Therefore, the regular expression should be match the following examples:

A123
A123B
#A123BNNN
.123
-123+

On the other side,AB123B123 is beyond the scope of the above pattern, since it has two sequences of digit values there. This question does not need to consider this scenario. Hopefully, I have made this question clear.

  • 1
    In your sample there are 2 strings with a non digit character both at start and end. In your description you state 'either'. Which is it? – forcefsck Nov 13 '13 at 21:33
  • Does a white character (ie space or tab) count as a non digit? Are all the values one per line? Do you need to match more than one string in the same line? What's the structure of your input file? Do you need to solve this in python? Do you care if it is a set of commands or has to be a single regex? You need to be as much specific as possible, otherwise you'll get an answer that works by luck and probably not an optimal. – forcefsck Nov 13 '13 at 22:29
  • I am more interested in the Regex part, using python, or shell command, or even other languages, such as Java or R are optional. Excuse me for the confusion. – user288609 Nov 14 '13 at 3:44
1

This seems to do what you want using grep:

$ grep -P "^\D+\d+$|^\d+\D+$|^\D+\d+\D+$" data.txt

Adding additional lines to data.txt would seem to indicate that this is working per your requirements.

Example

$ cat data.txt 
&320
*10-
AB123-
1234
ABCDEF
a20d34
12a12
32q5858j
' 2323 '
234aaa
#A123BNNN
.123
-123+

Sample run:

$ grep -P "^\D+\d+$|^\d+\D+$|^\D+\d+\D+$" data.txt
&320
*10-
AB123-
' 2323 '
234aaa
#A123BNNN
.123
-123+

If you're unfamiliar with grep's -P switch see the reference below. The switch allows for grep to use much of hte regex engine that's available in Perl.

References

  • this won't work for a20d34 or 12a12 or 32q5858j – forcefsck Nov 13 '13 at 22:17
  • @forcefsck - see updates, I think I've addressed what you mentioned. – slm Nov 13 '13 at 22:25
  • a20d34 and 32q5858j shouldn't match, there are non digits in the middle. If I understand correctly, there must be at least one non digit, either at the start or the end or both, but not in the middle. – forcefsck Nov 13 '13 at 22:32
  • @forcefsck - we need the OP to clarify, I don't see that requirement mentioned that there can't be any non-digit characters midstream. BTW, thank you for your constructive help! – slm Nov 13 '13 at 22:35
  • @sim, at this time, I am just considering the pattern where non-digit character can only occur at two positions: the very beginning and the very end. They can occur at both positions at the same time as well. Thanks. – user288609 Nov 14 '13 at 3:46
1

Your description seems to boil down to either one non-digit followed by a digit, or a digit followed by a non-digit.

grep -e '[^0-9][0-9]' -e '[0-9][^0-9]'
0
sed '
    /^[^0-9]\+[0-9]/b    # at least non-digit either before the first digit
    /[0-9][^0-9]\+$/b    # or after the last digit
    d
'

(edit: don't have to handle the no-digits case, that's ruled out by the problem statement.

  • I don't think these should be matched: a20d34 or 32q5858j, b/c of this "...If a numerical string does not follow this pattern...". See what I mean about this Q. W/o the OP you me and Stephane are just guessing on this? – slm Nov 14 '13 at 6:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.