regex


Simple Regular Expression matching


Im new to regular expressions and Im trying to use RegExp on gwt Client side. I want to do a simple * matching. (say if user enters 006* , I want to match 006...). Im having trouble writing this. What I have is :
input = (006*)
input = input.replaceAll("\\*", "(" + "\\" + "\\" + "S\\*" + ")");
RegExp regExp = RegExp.compile(input).
It returns true with strings like BKLFD006* too. What am I doing wrong ?
Put a ^ at the start of the regex you're generating.
The ^ character means to match at the start of the source string only.
I think you are mixing two things here, namely replacement and matching.
Matching is used when you want to extract part of the input string that matches a specific pattern. In your case it seems that is what you want, and in order to get one or more digits that are followed by a star and not preceded by anything then you can use the following regex:
^[0-9]+(?=\*)
and here is a Java snippet:
String subjectString = "006*";
String ResultString = null;
Pattern regex = Pattern.compile("^[0-9]+(?=\\*)");
Matcher regexMatcher = regex.matcher(subjectString);
if (regexMatcher.find()) {
ResultString = regexMatcher.group();
}
On the other hand, replacement is used when you want to replace a re-occurring pattern from the input string with something else.
For example, if you want to replace all digits followed by a star with the same digits surrounded by parentheses then you can do it like this:
String input = "006*";
String result = input.replaceAll("^([0-9]+)\\*", "($1)");
Notice the use of $1 to reference the digits that where captured using the capture group ([0-9]+) in the regex pattern.

Related Links

YAPE::Regex::Explain not working with use 5.014;
Optional JFlex lookahead with End of File
Regular expression that allows square-brackets
Reg Expression - split 2 string in between <h3> tags
Perl: Multiple lookbehind regex in a single regex expression
mod_rewrite to nginx rewrite rules
Perl Binary/PCAP regex
Why does grep match all lines for the pattern “\'”
regular expression for C if statementch
Regular expression for search name of methods in java files
Recommendation - Resource/book/pocketbook on regex in SED [closed]
Perl: composing regex thru interpolation when subexpr has capture
RegExp regular expression find & replace whole words only
Given a certain regex pattern, re-arrange and manipulate the contents of a cell
Regular expression“\\|” in strsplit
Parsing sectioned file with augeas

Categories

HOME
validation
collections
azure-web-sites
bootstrap-selectpicker
normalization
format
mvvm-light
sony
phpmailer
python-xarray
autofac
librsvg
cobol
jscrollpane
postgresql-9.5
condor
python-2.5
watchservice
orientdb-2.1
h.264
stm32f4discovery
deadbolt
ormlite-servicestack
core-location
yii1.x
code-signing
jtable
laravel-valet
python-responses
swipe
webcenter
advanced-installer
deep-copy
nav
msal
apartment-gem
flags
runge-kutta
amazon-elasticsearch
eve
csr
message-hub
plasticscm
sdcc
phasset
azure-cdn
asyncsocket
wrk
replaygain
ternary
canopen
reactivex
omniauth
curly-braces
mysql-error-1050
mit-scheme
heritrix
finalcut
restbed
bash-completion
flexigrid
folder-structure
monkeyrunner
polygons
double-quotes
chunk-templates
keypad
react-rails
gem5
vraptor
janino
excel-charts
bitrock
culture
bunny
deepzoom
angularjs-google-maps
fay
nsusernotificationcenter
gora
emacs-jedi
rte
rcu
layout-manager
cocosbuilder
code-structure
tipsy
program-files
jammit
soapheader
myisam
indextank
remote-control
xtratreelist
sqlitemanager
help-authoring
metaphone

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App