regular express issue with 1 character string
up vote
-1
down vote
favorite
I am allowing only alpha-numeric
, _
& -
values in string and removing all other characters. Its working fine but when string size 1 character (does not matter its alphabet
or numeric
or _
or -
), I got empty value instead of single charter.
Here is sample code
$str = 1;
$str = preg_replace('/^[a-zA-Z0-9_-]$/', '', $str);
var_dump($str);
or
$str = 'a';
$str = preg_replace('/^[a-zA-Z0-9_-]$/', '', $str);
var_dump($str);
I have tested this multiple versions of PHP as well
php regex preg-replace
|
show 6 more comments
up vote
-1
down vote
favorite
I am allowing only alpha-numeric
, _
& -
values in string and removing all other characters. Its working fine but when string size 1 character (does not matter its alphabet
or numeric
or _
or -
), I got empty value instead of single charter.
Here is sample code
$str = 1;
$str = preg_replace('/^[a-zA-Z0-9_-]$/', '', $str);
var_dump($str);
or
$str = 'a';
$str = preg_replace('/^[a-zA-Z0-9_-]$/', '', $str);
var_dump($str);
I have tested this multiple versions of PHP as well
php regex preg-replace
1
You might want to remove^
and$
and use'/[^a-zA-Z0-9_-]/'
(a negated character class) if you want to remove those chars you do not want anywhere in a string.
– Wiktor Stribiżew
Nov 9 at 20:35
After removing^
&$
result is same
– Hassaan
Nov 9 at 20:37
I meantpreg_replace('/[^a-zA-Z0-9_-]/', '', $str)
, =>string(1) "a"
– Wiktor Stribiżew
Nov 9 at 20:38
What is expected? You are replacing alpha numerical characters with nothing so an empty string is what I'd expect. Maybe usepreg_match
instead
– user3783243
Nov 9 at 20:39
1
If that works, you may also usepreg_replace('/[^w-]+/', '', $str)
– Wiktor Stribiżew
Nov 9 at 20:41
|
show 6 more comments
up vote
-1
down vote
favorite
up vote
-1
down vote
favorite
I am allowing only alpha-numeric
, _
& -
values in string and removing all other characters. Its working fine but when string size 1 character (does not matter its alphabet
or numeric
or _
or -
), I got empty value instead of single charter.
Here is sample code
$str = 1;
$str = preg_replace('/^[a-zA-Z0-9_-]$/', '', $str);
var_dump($str);
or
$str = 'a';
$str = preg_replace('/^[a-zA-Z0-9_-]$/', '', $str);
var_dump($str);
I have tested this multiple versions of PHP as well
php regex preg-replace
I am allowing only alpha-numeric
, _
& -
values in string and removing all other characters. Its working fine but when string size 1 character (does not matter its alphabet
or numeric
or _
or -
), I got empty value instead of single charter.
Here is sample code
$str = 1;
$str = preg_replace('/^[a-zA-Z0-9_-]$/', '', $str);
var_dump($str);
or
$str = 'a';
$str = preg_replace('/^[a-zA-Z0-9_-]$/', '', $str);
var_dump($str);
I have tested this multiple versions of PHP as well
php regex preg-replace
php regex preg-replace
edited Nov 10 at 12:53
asked Nov 9 at 20:34
Hassaan
4,91541337
4,91541337
1
You might want to remove^
and$
and use'/[^a-zA-Z0-9_-]/'
(a negated character class) if you want to remove those chars you do not want anywhere in a string.
– Wiktor Stribiżew
Nov 9 at 20:35
After removing^
&$
result is same
– Hassaan
Nov 9 at 20:37
I meantpreg_replace('/[^a-zA-Z0-9_-]/', '', $str)
, =>string(1) "a"
– Wiktor Stribiżew
Nov 9 at 20:38
What is expected? You are replacing alpha numerical characters with nothing so an empty string is what I'd expect. Maybe usepreg_match
instead
– user3783243
Nov 9 at 20:39
1
If that works, you may also usepreg_replace('/[^w-]+/', '', $str)
– Wiktor Stribiżew
Nov 9 at 20:41
|
show 6 more comments
1
You might want to remove^
and$
and use'/[^a-zA-Z0-9_-]/'
(a negated character class) if you want to remove those chars you do not want anywhere in a string.
– Wiktor Stribiżew
Nov 9 at 20:35
After removing^
&$
result is same
– Hassaan
Nov 9 at 20:37
I meantpreg_replace('/[^a-zA-Z0-9_-]/', '', $str)
, =>string(1) "a"
– Wiktor Stribiżew
Nov 9 at 20:38
What is expected? You are replacing alpha numerical characters with nothing so an empty string is what I'd expect. Maybe usepreg_match
instead
– user3783243
Nov 9 at 20:39
1
If that works, you may also usepreg_replace('/[^w-]+/', '', $str)
– Wiktor Stribiżew
Nov 9 at 20:41
1
1
You might want to remove
^
and $
and use '/[^a-zA-Z0-9_-]/'
(a negated character class) if you want to remove those chars you do not want anywhere in a string.– Wiktor Stribiżew
Nov 9 at 20:35
You might want to remove
^
and $
and use '/[^a-zA-Z0-9_-]/'
(a negated character class) if you want to remove those chars you do not want anywhere in a string.– Wiktor Stribiżew
Nov 9 at 20:35
After removing
^
& $
result is same– Hassaan
Nov 9 at 20:37
After removing
^
& $
result is same– Hassaan
Nov 9 at 20:37
I meant
preg_replace('/[^a-zA-Z0-9_-]/', '', $str)
, => string(1) "a"
– Wiktor Stribiżew
Nov 9 at 20:38
I meant
preg_replace('/[^a-zA-Z0-9_-]/', '', $str)
, => string(1) "a"
– Wiktor Stribiżew
Nov 9 at 20:38
What is expected? You are replacing alpha numerical characters with nothing so an empty string is what I'd expect. Maybe use
preg_match
instead– user3783243
Nov 9 at 20:39
What is expected? You are replacing alpha numerical characters with nothing so an empty string is what I'd expect. Maybe use
preg_match
instead– user3783243
Nov 9 at 20:39
1
1
If that works, you may also use
preg_replace('/[^w-]+/', '', $str)
– Wiktor Stribiżew
Nov 9 at 20:41
If that works, you may also use
preg_replace('/[^w-]+/', '', $str)
– Wiktor Stribiżew
Nov 9 at 20:41
|
show 6 more comments
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
You are removing any chars other than ASCII letters, digits, _
and -
anywhere inside the string. You need to remove anchors and convert the positive character class into a negated one:
$str = preg_replace('/[^w-]+/', '', $str);
See the PHP demo online and a regex demo.
Details
[^
- start of a negated character classw
- a word char: letter, digit or_
-
- a hyphen
]
- end of the character class+
- a quantifier: 1 or more repetitions.
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
You are removing any chars other than ASCII letters, digits, _
and -
anywhere inside the string. You need to remove anchors and convert the positive character class into a negated one:
$str = preg_replace('/[^w-]+/', '', $str);
See the PHP demo online and a regex demo.
Details
[^
- start of a negated character classw
- a word char: letter, digit or_
-
- a hyphen
]
- end of the character class+
- a quantifier: 1 or more repetitions.
add a comment |
up vote
1
down vote
accepted
You are removing any chars other than ASCII letters, digits, _
and -
anywhere inside the string. You need to remove anchors and convert the positive character class into a negated one:
$str = preg_replace('/[^w-]+/', '', $str);
See the PHP demo online and a regex demo.
Details
[^
- start of a negated character classw
- a word char: letter, digit or_
-
- a hyphen
]
- end of the character class+
- a quantifier: 1 or more repetitions.
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
You are removing any chars other than ASCII letters, digits, _
and -
anywhere inside the string. You need to remove anchors and convert the positive character class into a negated one:
$str = preg_replace('/[^w-]+/', '', $str);
See the PHP demo online and a regex demo.
Details
[^
- start of a negated character classw
- a word char: letter, digit or_
-
- a hyphen
]
- end of the character class+
- a quantifier: 1 or more repetitions.
You are removing any chars other than ASCII letters, digits, _
and -
anywhere inside the string. You need to remove anchors and convert the positive character class into a negated one:
$str = preg_replace('/[^w-]+/', '', $str);
See the PHP demo online and a regex demo.
Details
[^
- start of a negated character classw
- a word char: letter, digit or_
-
- a hyphen
]
- end of the character class+
- a quantifier: 1 or more repetitions.
answered Nov 9 at 20:46
Wiktor Stribiżew
301k16122197
301k16122197
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53232920%2fregular-express-issue-with-1-character-string%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
You might want to remove
^
and$
and use'/[^a-zA-Z0-9_-]/'
(a negated character class) if you want to remove those chars you do not want anywhere in a string.– Wiktor Stribiżew
Nov 9 at 20:35
After removing
^
&$
result is same– Hassaan
Nov 9 at 20:37
I meant
preg_replace('/[^a-zA-Z0-9_-]/', '', $str)
, =>string(1) "a"
– Wiktor Stribiżew
Nov 9 at 20:38
What is expected? You are replacing alpha numerical characters with nothing so an empty string is what I'd expect. Maybe use
preg_match
instead– user3783243
Nov 9 at 20:39
1
If that works, you may also use
preg_replace('/[^w-]+/', '', $str)
– Wiktor Stribiżew
Nov 9 at 20:41