正则表达式用\ 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
最满意答案
我可以建议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 usingexecuteUpdate()
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.
相关问答
更多-
如何把它放在jframe形式(how to put this in jframe form)[2022-06-24]
我觉得我不应该这样做,因为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. ...