-3

I have this text:

1234

I want to use a regex to select all except the first digit (in this case 1) - so a pattern that selects

234

How can I do this? I can't use variables, only regex. And also I can't delete the first digit. Only a regex that select all the string except the first number.

closed as unclear what you're asking by icarus, Mr Shunz, muru, roaima, Jeff Schaller Apr 19 at 23:31

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    Maybe you want sed 's/\([1234]\)\([1234][1234][1234]\)/ first is \1, rest is \2/' but it is very hard to tell from your question. Please clarify. – icarus Apr 18 at 5:49
  • add some more information in your question. Is it only digit ? or alphanumeric ?.. only one word/column in your text..etc – Kamaraj Apr 18 at 7:24
  • 2
    Please could you clarify what you're trying to do? Where do you "have" this text (in a file? the output of some command?), and what do you want to happen after you "select" it (save it in a variable? redirect it to another file, or command? copy it to the clipboard?) – JigglyNaga Apr 18 at 9:16
  • Which tool are you using? – muru Apr 18 at 14:49
  • a=1234; echo "${a:1}" also returns 234. It really depends what you want to achieve. – roaima Apr 19 at 0:04
2

Assuming the text is in the file file, the following sed command will remove the first digit on each line of the file and output the result:

sed 's/[[:digit:]]//' file

Testing:

$ cat file
123
1234
alpha123
a1b2c3
$ sed 's/[[:digit:]]//' file
23
234
alpha23
ab2c3

If the string is in a variable, you don't really need a regular expression. It would be enough with

${string/[[:digit:]]/}

in bash.

$ string=alpha123
$ printf '%s\n' "${string/[[:digit:]]/}"
alpha23

If you're only interested in removing a digit if it occurs in the first position, you may use the standard parameter substitution

${string#[[:digit:]]}

as in

$ string=1234
$ printf '%s\n' "${string#[[:digit:]]}"
234
2

The standard command to apply regexps to string arguments is expr with its : operator. It understands standard Basic Regular Expressions. It outputs 1 or 0 depending on whether the regexp matches or not unless the regexp has at least one capture group in which case it outputs what was matched by the first capture group. One peculiarity of expr's : is that the regexp is implicitly anchored at the start as if it started with ^. So:

text=1234
expr "x$text" : "x.\(.*\)"

We prefix both the text and regexp with x (arbitrary), as otherwise the command would fail if the content of $text happened to be an expr operator.

The first . matches the first character of $text which is not output as it's not inside the capture group. Then we capture the rest .* being 0 or more characters, as many as possible for output.

The exit status will be non-zero if the regexp doesn't match ($text is empty or starts with something that can't be interpreted as a character) or if the output is the number 0 (and depending on the expr implementation, some of its various other spellings like 00, -0...) or the empty string.

But in any case, you don't need to run expr or use regexps for that. Standard shell parameter expansion operators can do it:

text=1234
printf '%s\n' "${text#?}"

Where ${var#pattern} expands to the content of $var where the leading part that matches pattern has been removed.

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