Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

regular expression pattern difference between [\.\-] and [\-\.]

Mustafa KALAYCIMay 8 2018 — edited May 8 2018

Hello everyone,

I am here to ask another regular expression question again here is my sample code:

select regexp_substr('6-3', '[0-9][/\-\.][0-9from dual;

result is null.

in square brackets, we write char list that can match. so I want to check if the punctuation char is a dot, hyphen or slash but if I changed their place:

select regexp_substr('6-3', '[0-9][/\.\-][0-9from dual;

result is 6-3

since backslash is escape char I am using it before hyphen but it seems it is treated as "range" operator anyway. is that correct?

also this is not just working when source string has hyphen, if I change it to dot, it is working on every pattern.

also when I use (\-|\.) is working again.

edit: while writing this question, I tried [\-/\.] and I got "invalid range" error. so hyphen in square brackets is always treated as range operator is this correct? of course except it is not at the end of the list, then there is no range and it is working.

thanks.

This post has been answered by Paulzip on May 8 2018
Jump to Answer

Comments

camickr
In JDK1.4.2 it only supports insert mode.

Is there a new feature in 1.5?
843805
In JDK1.4.2 it only supports insert mode.

Is there a new feature in 1.5?
I don't know about JTextPane in JDK1.5.
And I agree with you that JTextPane supports insert mode only in JDK1.4.2 but I need to have overwrite mode too in my JTextPane.
What should I do?
800351
An idea:
Override insertString() and do continuously remove/replace pre-exixting characters.
camickr
but I need to have overwrite mode too in my JTextPane
Then why didn't you state that in your original question?

Here is my version that allows you to toggle between both modes:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;
import javax.swing.event.*;
import javax.swing.plaf.*;

public class OvertypeTextArea extends JTextArea
{
	private static Toolkit toolkit = Toolkit.getDefaultToolkit();
	private static boolean isOvertypeMode;

	private Caret defaultCaret;
	private Caret overtypeCaret;

	public OvertypeTextArea(int row, int column)
	{
		super(row, column);
		setCaretColor( Color.red );
		defaultCaret = getCaret();
		overtypeCaret = new OvertypeCaret();
		overtypeCaret.setBlinkRate( defaultCaret.getBlinkRate() );
		setOvertypeMode( true );
	}

	/*
	 *	Return the overtype/insert mode
	 */
	public boolean isOvertypeMode()
	{
		return isOvertypeMode;
	}

	/*
	 *	Set the caret to use depending on overtype/insert mode
	 */
	public void setOvertypeMode(boolean isOvertypeMode)
	{
		this.isOvertypeMode = isOvertypeMode;
		int pos = getCaretPosition();

		if ( isOvertypeMode() )
		{
			setCaret( overtypeCaret );
		}
		else
		{
			setCaret( defaultCaret );
		}

		setCaretPosition( pos );
	}

	/*
	 *  Override method from JComponent
	 */
	public void replaceSelection(String text)
	{
		//  Implement overtype mode by selecting the character at the current
		//  caret position

		if ( isOvertypeMode() )
		{
			int pos = getCaretPosition();

			if (getSelectedText() == null
			&&  pos < getDocument().getLength())
			{
				moveCaretPosition( pos + 1);
			}
		}

		super.replaceSelection(text);
	}

	/*
	 *  Override method from JComponent
	 */
	protected void processKeyEvent(KeyEvent e)
	{
		super.processKeyEvent(e);

	 	//  Handle release of Insert key to toggle overtype/insert mode

	 	if (e.getID() == KeyEvent.KEY_RELEASED
	 	&&  e.getKeyCode() == KeyEvent.VK_INSERT)
	 	{
			setOvertypeMode( ! isOvertypeMode() );
	 	}
	}

	/*
	 *  Paint a horizontal line the width of a column and 1 pixel high
	 */
	class OvertypeCaret extends DefaultCaret
	{
		/*
		 *  The overtype caret will simply be a horizontal line one pixel high
		 *  (once we determine where to paint it)
		 */
		public void paint(Graphics g)
		{
			if (isVisible())
			{
				try
				{
					JTextComponent component = getComponent();
					TextUI mapper = component.getUI();
					Rectangle r = mapper.modelToView(component, getDot());
					g.setColor(component.getCaretColor());
					int width = g.getFontMetrics().charWidth( 'w' );
					int y = r.y + r.height - 2;
					g.drawLine(r.x, y, r.x + width - 2, y);
				}
				catch (BadLocationException e) {}
			}
		}

		/*
		 *  Damage must be overridden whenever the paint method is overridden
		 *  (The damaged area is the area the caret is painted in. We must
		 *  consider the area for the default caret and this caret)
		 */
		protected synchronized void damage(Rectangle r)
		{
			if (r != null)
			{
				JTextComponent component = getComponent();
				x = r.x;
				y = r.y;
				width = component.getFontMetrics( component.getFont() ).charWidth( 'w' );
				height = r.height;
				repaint();
			}
		}
	}

	public static void main(String[] args)
	{
		JFrame frame = new JFrame();
		frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
		OvertypeTextArea textArea	= new OvertypeTextArea(5,20);
		textArea.setFont( new Font("monospaced", Font.PLAIN, 12) );
		textArea.setText("abcdefg");
		frame.getContentPane().add( textArea );
		frame.pack();
		frame.setVisible(true);
	}
}
843805
thanks camickr your code worked great.
camickr
Glad you found the code helpfull.
1 - 6
Locked Post
New comments cannot be posted to this locked post.

Post Details

Locked on Jun 5 2018
Added on May 8 2018
12 comments
1,674 views