Remove text from a text file without otherwise altering the file

1 次查看(过去 30 天)
I need to remove text from a text file without otherwise altering the file INCLUDING other text in the same line. specifically I need the text of the form 'E\S+' (ie "E3453.464") to go away without otherwise altering the line.
so this: G1 X104.650 Y95.350 E4.58979 should become this G1 X104.650 Y95.350
and this G1 E-2.00000 F2400.00000 should become this G1 F2400.00000

采纳的回答

Paolo
Paolo 2018-6-11
编辑:Paolo 2018-6-11
You can use regexprep for this purpose.
x1 = 'G1 X104.650 Y95.350 E4.58979';
x2 = 'G1 E-2.00000 F2400.00000';
x1 = regexprep(x1,'(E-?)([0-9]){0,}\.?(\d+)\s?','');
x2 = regexprep(x2,'(E-?)([0-9]){0,}\.?(\d+)\s?','');
x1 = 'G1 X104.650 Y95.350 '
x2 = 'G1 F2400.00000'
The expression:
  • Match E
  • Match '-' optionally
  • Match numbers 0-9 at least one time
  • Match '.' optionally
  • Match as many digits as possible
  • Match whitespace optionally
  6 个评论
Guillaume
Guillaume 2018-6-11
Note that {0,} is the same as * and {1,} is the same as + in regular expressions. In my opinion, E-{0,} should be E-? (aka E-{0,1})
Paolo
Paolo 2018-6-11
编辑:Paolo 2018-6-11
Totally, the first E-{0,} is actually meant to be E-?, as from the examples OP gave there is only one '-' character. While editing I changed both of them rather than just the second one...

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Environment and Settings 的更多信息

产品


版本

R2016a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by