开发者

Java regex and sed aren't the same...?

开发者 https://www.devze.com 2023-03-06 23:09 出处:网络
Get these strings: 00543515703528 00582124628575 0034911320020 0034911320020 005217721320739 0902345623 0679131开发者_StackOverflow社区87056

Get these strings:

00543515703528
00582124628575
0034911320020
0034911320020
005217721320739
0902345623
0679131开发者_StackOverflow社区87056
00543515703528

Apply this exp in java: ^(06700|067|00)([0-9]*).

My intention is to remove leading "06700, 067 and 00" from the beggining of the string.

It is all cool in java, group 2 always have the number I intend to, but in sed it isnt the same:

$ cat strings|sed -e 's/^\(06700|067|00\)\([0-9]*\)/\2/g'
00543515703528
00582124628575
0034911320020
0034911320020
005217721320739
0902345623
067913187056
00543515703528

What the heck am I missing?

Cheers,

f.


When using extended regular expressions, you also need to omit the \ before ( and ). This works for me:

sed -r 's/^(06700|067|00)([0-9]*)/\2/g' strings 

note also that there's no need for a separate call to cat


I believe your problem is this:

sed defaults to BRE: The default behaviour of sed is to support Basic Regular Expressions (BRE). To use all the features described on this page set the -r (Linux) or -E (BSD) flag to use Extended Regular Expressions

Source

Without this flag, the | character is interpreted literally. Try this example:

echo "06700|067|0055555" | sed -e 's/^\(06700|067|00\)\([0-9]*\)/\2/g'
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号