Regarding patterns and regex
Hi,
I want to check that my variable doesnot have anything except characters and numbers..i tried implementing it...
test="ABC12354YR89"
if(Pattern.matches("[a-z_0-9A-Z_0-9]", test))
......
but this doesnot work..
i need guidanace as to what regex shall i use to check for special characters..
as always...thanks for your prompt responses...
cheers../
[406 byte] By [
chabhia] at [2007-11-27 4:31:59]

Also, I've used this app to test regex strings. You may find it helpful:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class RegexNumbers extends JFrame
{
JTextField regexField = new JTextField(20);
JTextField toTestField = new JTextField(20);
JTextField resultField = new JTextField(20);
JButton doTestBtn = new JButton("Test Regex");
JPanel mainPane;
public RegexNumbers()
{
GridLayout mainGL = new GridLayout(2, 1);
mainGL.setHgap(10);
mainGL.setVgap(10);
mainPane = new JPanel(mainGL);
JLabel regexString = new JLabel("Regex String");
regexString.setHorizontalAlignment(SwingConstants.CENTER);
JLabel stringToTest = new JLabel("String To Test");
stringToTest.setHorizontalAlignment(SwingConstants.CENTER);
JLabel resultStr = new JLabel("Result");
resultStr.setHorizontalAlignment(SwingConstants.CENTER);
doTestBtn.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
doTestBtnAction();
}
});
resultField.setEditable(false);
GridLayout upperPaneGL = new GridLayout(2, 3);
upperPaneGL.setHgap(10);
upperPaneGL.setVgap(10);
JPanel upperPane = new JPanel(upperPaneGL);
upperPane.add(regexField);
upperPane.add(toTestField);
upperPane.add(resultField);
upperPane.add(regexString);
upperPane.add(stringToTest);
upperPane.add(resultStr);
JPanel upperOuterPane = new JPanel();
upperOuterPane.add(upperPane);
upperOuterPane.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
JPanel lowerPane = new JPanel();
lowerPane.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
lowerPane.add(doTestBtn);
mainPane.add(upperOuterPane);
mainPane.add(lowerPane);
}
private void doTestBtnAction()
{
String regexString = regexField.getText();
String stringToTest = toTestField.getText();
boolean regexResult = stringToTest.matches(regexString);
resultField.setText(Boolean.toString(regexResult));
}
private void createGUI()
{
//JFrame mainFrame = new JFrame("Regex Tester");
setTitle("Regex Tester");
getContentPane().add(mainPane);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setVisible(true);
}
private static void createAndShowGUI()
{
RegexNumbers rn = new RegexNumbers();
rn.createGUI();
}
public static void main(String[] args)
{
javax.swing.SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
createAndShowGUI();
}
});
}
}