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

Simplify a regex - multiple discrete options
Redirecting case insensitibe URL in apache
PostgreSQL regex check start with Latin and alpha character
Antlr4 define a operator which can be Unary or Binary
How to get a group value from grep?
VS 2013 can't find lib file from Boost toolkit
Image files have got changed but pictures look the same
REGEX in perl, start matching from the n occurrence of elem e
Regex - Include up to a certain character and exclude occurences after
Regex pattern to match groups starting with pattern
Python3 Double Quote Parsing and Detection
Regular expression for finding a substring not surrounded by other characters [duplicate]
How to write regex for redirection
Read sections from Manual Page using Terminal man command
Parse Args with R.E
Regex Extract All using pig

Categories

HOME
ios
opencv
performance
f#
ffmpeg
wix
runnable
orbeon
auth0
path
image-segmentation
amazon-kinesis
stack
streaming
antlr
go-gorm
build.gradle
player-swift
add-in
doorkeeper
executable
cytoscape.js
deserialization
spring-cloud-consul
next
syswow64
round-robin
javers
gas
silk-performer
ghost-blog
liferay-aui
shape
epicorerp
italic
weather-api
intersystems
masstransit
sql-server-2014-express
shinydashboard
akka-http
nest-thermostat
complex-networks
tensor
google-crawlers
phing
ecmascript-4
vtable
signing
windows-scripting
procobol
polymer-cli
formatdatetime
permgen
freerdp
zfs
sdcc
php-deployer
opal
multistore
sql-mode
ios-app-group
canopen
g-wan
mysql-error-1050
turbo-c
android-alertdialog
turnjs
vb4android
sizeof
data-fitting
blpapi
mars
java-gstreamer
metalkit
thinktecture-ident-server
autopy
python-dragonfly
many-to-one
monkeyrunner
pegkit
keypad
fastparse
sigkill
vendor
android-design-library
computer-science-theory
jet.com-apis
httpservice
dynamic-data-display
django-supervisor
nitrogen
dojo.gridx
hobbitmon
flexpaper
google-goggles
java-ee-5
onmouseout
xml-dtd
obout
j#
directoryentry
paredit
renderaction
symstore
noaa
regexbuddy
automapping
wmd
non-relational-database
contracts
68hc11
web-based
xlink
mud

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