首页 \ 问答 \ 正则表达式用\ r \ n替换\ r后面没有后跟\ n的所有实例(Regex to replace all instances of \r not followed by \n with \r\n)

正则表达式用\ r \ n替换\ r后面没有后跟\ n的所有实例(Regex to replace all instances of \r not followed by \n with \r\n)

\r\n后面没有使用\n表达式的\n后,如何用\r\n替换\r

问题

我在Windows上创建一个文本编辑器时出现问题,因为换行符被添加为\ r \ n。 因此,当我在记事本中打开文件时,它们不会被读为换行符。

我尝试了什么

我已经尝试到处寻找(谷歌,堆栈溢出等),但一直无法找到专门为我所追求的东西。

到目前为止我尝试过的所有东西都是我一次做的事情,但是它会不必要地替换\r ,即使它们后面跟着一个\n

供参考,这是表达式:

"\r(?!^\n)", "\r\n"

How can I replace \r's with \r\n when the \r isn't followed by a \n using Regex ?

The problem

I'm having problems with a text editor on Windows I'm creating in that line breaks are being added as \r. As a result they are not being read as line breaks when I open the file in Notepad.

What I tried

I've tried looking everywhere (google, stackoverflow, etc) for this, but have been unable to find something that is specifically for what I'm after.

Everything I've tried so far does what I'm after the first time but it then keeps unnecessarily replacing \r's even if they're followed by a \n.

For reference, this is the expression:

"\r(?!^\n)", "\r\n"

原文:https://stackoverflow.com/questions/41423081
更新时间:2022-01-27 12:01

最满意答案

我可以建议2种基本方法(还有其他选择)。
根据特定的设计和上下文需求来选择它们。

第一个 - 允许仅删除有效记录(推荐)。
此方法限制了允许的操作,仅允许有效操作。
例如,这可以通过从有效选项的只读列表中选择ID来完成。 一旦删除 - ID就会被删除,并且不再出现在GUI中。

第二个 - 允许所有内容,但在执行之前或之后检查,然后(在两种情况下),向用户显示适当的消息。
例如,这可以通过使用executeUpdate()来完成,该函数返回已更改的行数(在您的情况下删除),如果没有受影响,则返回0。

这两个选项都被广泛用于限制用户交互并提供基本指导。
但是,在CRUD应用程序中,您需要仔细考虑任一选项如何影响您的应用程序,以及这两种实现的结果。


I can suggest 2 basic approaches (there are additional options ofcourse).
Choosing between them is done per specific design and context needs.

The first - allow to delete only valid records (recommended).
This approach limits the allowed operations and permits only valid operations.
This can be done, for example, by selecting the IDs from a read-only list of valid options. Once deleted - the ID is simply removed and does not appear anymore in the GUI.

The second - allow everything, but check either before execution or after the fact, and then (in both cases), display an appropriate message to the user.
This can be done, for example, by using executeUpdate() which returns the number of rows which were changed (deleted in your case), or 0 if none were affected.

Both options are widely used to limit user interactions and provide basic guiding.
In CRUD applications, however, you need to consider carefully how either option can affect your application, and what would be the results of either implementation.

相关问答

更多
  • 我觉得我不应该这样做,因为SO不是代码服务网站,但是嘿! 这是一个例子! (看起来不太好,并将NGN更改为EUR) import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.URL; import jav ...
  • 一种简单的方法是为JPanel类提供一个委托方法,允许其他类将ActionListeners添加到感兴趣的按钮。 例如: public void addButton1ActionListener(ActionListener listener) { button1.addActionListener(listener); } 这样,如果JFrame包含JPanel的实例,它可以调用此方法将侦听器传递给JButton。 例如: import java.awt.BorderLayout; import ...
  • 你只需要添加revalidate(); 喜欢这个: private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: int c; c=Integer.parseInt ( jTextField2.getText()) ; jTextField1.s ...
  • 怎么样 class MyGUI extends JFrame { public MyGUI() { setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);// <- prevent closing final MyGUI gui=this; addWindowListener(new WindowAdapter() { public void windowClosing(W ...
  • 如果您已经添加了必要的空间,对于框架窗体的正确JPanel ,只需手动编写添加JPanel表单的问题。 这样的事情 public FrameForm extends JFrame { private PanelForm pForm = new PanelForm(); public FrameForm() { initComponents(); CardLayout card = new CardLayout(); rightPanel.se ...
  • 您是否尝试在表单包含字段中添加侦听器? 我知道你没有完全控制那个类,但是,这是一些java代码! 在调试器中运行它,您将看到该DataWindow的代码组织。 然后,您将能够添加一个Key / Action / Anything / Listener来触发代码执行。 Have you tried adding a listener in form contained fields ? I know you don't have full control over that class, but, well, ...
  • 我会假设你使用netbeans很抱歉,如果我错了(你没说)。 有两种方法可以做到这一点,第一种方法是为新的JFrame创建一个单独的表单(右键单击> New> JFrame),然后在单击按钮时创建一个新的实例。 第二个选项是在GUI构建器中添加它,如果你这样做它最初不会显示,你必须到左边有一个组件列表,然后点击其他组件> jframe1使它出现(这将隐藏您可以通过单击导航器中的主窗体组件隐藏的主窗体。 I will assume your using netbeans so sorry if I'm wro ...
  • 假设你的按钮有一个actionListener,在点击“规则按钮”后输入: JFrame1.dispose(); //Remove JFrame 1 JFrame2.setVisible(true) //Show other frame 然后尊重他们反对的反应 Assuming your button has an actionListener, after clicking the "rules button" put in: JFrame1.dispose(); ...
  • 我可以建议2种基本方法(还有其他选择)。 根据特定的设计和上下文需求来选择它们。 第一个 - 允许仅删除有效记录(推荐)。 此方法限制了允许的操作,仅允许有效操作。 例如,这可以通过从有效选项的只读列表中选择ID来完成。 一旦删除 - ID就会被删除,并且不再出现在GUI中。 第二个 - 允许所有内容,但在执行之前或之后检查,然后(在两种情况下),向用户显示适当的消息。 例如,这可以通过使用executeUpdate()来完成,该函数返回已更改的行数(在您的情况下删除),如果没有受影响,则返回0。 这两个选 ...
  • 有两种方法可以解决这个问题。 a)使JFrame不可调整大小。 这不会删除最大化按钮,而是会禁用它。 JFrame theFrame = new JFrame(); theFrame.setSize(300,300); theFrame.setResizable(false); theFrame.setVisible(true); b)改为使用JDialog。 希望这能回答你的问题 There are two ways that you can approach this. ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)